Issues

ZF-11801: formFile ViewHelper renders id /name attribute not correct when in subform

Description

when using a Zend_Form_Element_File - element inside of a fieldset, the fieldset prefix is not added to the name and id of the rendered file input tag.


$preview_image = new Zend_Form_SubForm();
$preview_image->setLegend('Preview Image');
              
        $test_file = new Zend_Form_Element_File('test_file');
        $test_file->setLabel('Test Label');
        $preview_image->addElement($test_file);

$this->addSubForms(array(
    'preview_image' => $preview_image
));

Preview Image
Test Label

The id and name attribute of the file input should be "preview_image-test_file" just like the label's for attribute value. And of course the name should be the FullyQualifiedName "preview_image[test_file]". At least the ViewHelpers in all other Form Elements do behave that way.

If I set the 'ViewHleper' as additional Decorator, the output of the ViewHelper (uses 'formFile' in this case) is right. But since I have to use the 'File' Decorator also ( I don't understand why ), the output of this one is wrong.

Comments

I've found a workaround for this bug: - build an own File Element Class:

class My_Form_File extends Zend_Form_Element_File { // public function render(Zend_View_Interface $view = null) {

     /* 
     * this will set the right id (with filedset-id prefix just like the Label Deorator's "for"-attribute-value) and name attribute (array notation with fieldset prefix"
     */

     $this->getDecorator('File')->setOptions(array('name' => $this->getFullyQualifiedName(),'id' => $this->getId() ));

   return parent::render($this->getView());
}

}

rendered output: Example:

Test Bild
  • Bitte wählen Sie ein entsprechendes Bild:
    .jpg .png .gif / min. 800px*800px

Code tags added.

From the manual: {quote} Note: File elements in Subforms When you use file elements in subforms you must set unique names. For example, if you name a file element in subform1 "file", you must give any file element in subform2 a different name. If there are 2 file elements with the same name, the second element is not be displayed or submitted. Additionally, file elements are not rendered within the sub-form. So when you add a file element into a subform, then the element will be rendered within the main form. {quote}

This is the reason why the file element is not rendered within the subform "preview_image". The restriction is due to nested file uploads being broken.