Subforms and multiple file elements
- Posted by thomasw at 20:29:25 // //
- Announcements, Filter
Hy interested ones,
when you are using Zend_Form for your file upload I have new informations for you.
*) Subforms
Using file elements in sub forms was not possible in the past. The reason was that sub forms are created by using multidimensional names, but in HTML multidimensional names are not supported for file elements.
Now this is possible. I have added a new decorator with the help of Matthew which is automatically be used for file elements. It changes the elements name in a way which is supported by HTML.
As you can see in the following example, even multi-nested subforms are supported.
$form = new Zend_Form();
$form->setEnctype('multipart/form-data');
$form->setAction('index2.php');
// Button
$button = new Zend_Form_Element_Submit('submit');
$form->addElement($button);
// File Upload Elements
$element = new Zend_Form_Element_File('firstfile');
$element2 = new Zend_Form_Element_File('secondfile');
// 3-Level subform
$subform0 = new Zend_Form_SubForm();
$subform0->addElement($element);
$subform0->addElement($element2);
$subform1 = new Zend_Form_SubForm();
$subform1->addSubform($subform0, 'subform0');
$subform2 = new Zend_Form_SubForm();
$subform2->addSubform($subform1, 'subform1');
$subform3 = new Zend_Form_SubForm();
$subform3->addSubform($subform2, 'subform2');
$form->addSubform($subform3, 'subform3');
$form->setView(new Zend_View());
echo $form;
But note that the name of the file elements you set in a form and all it’s subforms must be unique. File elements with the same name are displayed but not submitted by your form.
*) Multiple file elements
Sometimes it is usefull to have multiple file elements which uses the same settings and validators. Writing plain HTML you would simply set a name like filename[] several times. Zend_Form_Element_File can handle this for you much simpler. Just create your element like before and call setMultiFile() with the number of files you want to have.
$element = new Zend_Form_Element_File("uploaded");
$element->setMultiFile(3);
The above example will create 3 seperated file elements named uploaded[].
I hope you find these two features handy.
New things will come soon.
Greetings
Thomas, I18N Team Leader, Zend Framework
