ZF-5864: Zend_Form_Element_File in sub form: name attribute is not rendered using the array notation

Description

When creating a file element in a sub form the name attribute should use the array notation in this form: sub_form[element_name] but this work.

I have the following two elements in a sub form: $image_src = new Zend_Form_Element_File ('image_src'); $this->addElement($image_src);

$image_alt = new Zend_Form_Element_Text('image_alt'); $this->addElement($image_alt);

The elements as rendered in the sub form look like this:

Comments

Assigning to Thomas. Thomas, make sure the decorators are using getFullyQualifiedName().

@G.: I just verified but I can't get your results. When I name the file element "image_src" the rendered element is also named "image_src" and not "image".

Related to the sub_form notation: This is nothing which can be done for file elements.

The problem arises when you use Sub-Sub-Forms, or deeper nesting, or when you use multifiles with Sub-Forms. PHP then brakes the Sub-Form notation into something unreadable. For example a file within a 3 level nested Sub-Form would be named "subform3[subform2][subform1][subform0][image_src]". But the resulting $_FILES array is Array ([subform3] => Array ([name] => Array ([subform2] => Array ([subform1] => Array ([subform0] => Array ([image_src] => filename)))) As the resulting name is broken in multiple sub arrays the upload is broken.

This is within PHP itself a not defined behaviour. And this behaviour is different depending on the used PHP version. Therefor we change the naming of file elements to match the accepted PHP naming notation.

With the actual PHP releases there is no known solution to this problem as it's simply not defined.

This is not a bug, this is wished behaviour and I think it's also documented.

Thank you for responding so quickly.

The first "issue" is my own fault as I was trying to simplify the code for this report and got it wrong. I do get image_src as the element name.

The issue I was addressing is that I am expecting the file element to be name "image_form['image_src]" (if my sub form is named image_form) and for it to be part of the sub form array when I request the form data when I process it. This is (simplified again) what I am expecting:

$form_data = $this->_request->getPost(); $image_data = $form_data['image_form'];

but $image_form does not contain the data (file name) from the file element, so what I have to do ($main_form is my main form): $image_form = $main_form->getSubForm('image_form'); $image_data = $image_form->getValues();

I only have one sub form to main form (I'm not so ambitious as to have sub forms nested in sub forms).

Obviously, it's not a big deal and the workaround doesn't require a lot of work, but I still think it is a bug as I was expecting it to behave in the same way as other sub form elements but it and I think the idea of array notation is very useful, but not if the file element is missing from it.

If it's impossible for the file element to be added to the array notation in sub forms then I think the documentation (http://framework.zend.com/manual/en/…) should be changed to reflect that.

Thanks again for your time.

Golan.

Fixed with r16002 (documentation).