ZF-2563: Allow array notation in form element names
A common pattern used by those developing forms is to assign elements as array keys:
This is done to group related data, as well as on forms that may have repeated sets of form data (e.g., a 'todo' application, that might create a new set of data for each todo item).
Currently, this is not supported in Zend_Form, and the support within Zend_View_Helper_FormElement is slightly buggy.
In Zend_View_Helper_FormElement can create names with array notation without a problem. However, since the name is used as the id when no id is present, this causes invalid ids. Additionally, it does not currently look to see if the provided value is an array, nor if the element's name exists in the array, leading to errors and buggy output.
Within Zend_Form, we cannot allow names with array notation due to the fact that we overload; array notation will cause interpreter issues with PHP.
The solution to this is to use sub forms. My proposal is to add a 'belongsTo' property to Zend_Form_Element. SubForms, when rendering, would then set the 'belongsTo' property of each element. Decorators that need to be array-aware would then build the name using the value of this property:
$name = $element->getBelongsTo() . '[' . $element->getName() . ']';
Additionally, it could set the id, if not previously set, by concatenating the belongsTo property and the name with a dot separator:
$id = $element->getBelongsTo() . '.' . $element->getName();
Finally, the value would be passed by the decorator based on the element value, thus bypassing the issues in Zend_View_Helper_FormElement.