Sometimes, you may want to add input (or a set of inputs) multiple times, either because you don’t want to duplicate code, or because you does not know in advance how many elements you need (in the case of elements dynamically added to a form using JavaScript, for instance).
Zend\Form\Element\Collection is meant to be paired with the Zend\Form\View\Helper\FormCollection.
1 2 3 4 5 6 7 8 9 10 11 | use Zend\Form\Element;
use Zend\Form\Form;
$colors = new Element\Collection('collection');
$colors->setLabel('Colors');
$colors->setCount(2);
$colors->setTargetElement(new Element\Color());
$colors->setShouldCreateTemplate(true);
$form = new Form('my-form');
$form->add($colors);
|
The following methods are in addition to the inherited methods of Zend\Form\Element .
Set options for an element of type Collection. Accepted options, in addition to the inherited options of Zend\Form\Element <zend.form.element.methods.set-options>` , are: "target_element", "count", "allow_add", "should_create_template" and "template_placeholder" , which call setTargetElement, setCount, setAllowAdd, setShouldCreateTemplate and setTemplatePlaceholder , respectively.
Defines how many times the target element will be rendered by the Zend/Form/View/Helper/FormCollection view helper.
Return the number of times the target element will be initially rendered by the Zend/Form/View/Helper/FormCollection view helper.
Return type: | integer |
---|
This function either takes an Zend/Form/ElementInterface, Zend/Form/FieldsetInterface instance or an array to pass to the form factory. When the Collection element will be validated, the input filter will be retrieved from this target element and be used to validate each element in the collection.
Return the target element used by the collection.
Return type: | ElementInterface | null |
---|
If allowAdd is set to true (which is the default), new elements added dynamically in the form (using JavaScript, for instance) will also be validated and retrieved.
Return if new elements can by dynamically added in the collection.
Return type: | boolean |
---|
If allowRemove is set to true (which is the default), new elements added dynamically in the form (using JavaScript, for instance) will be allowed to be removed.
Return if new elements can by dynamically added in the collection.
Return type: | boolean |
---|
If shouldCreateTemplate is set to true (defaults to false), a <span> element will be generated by the Zend/Form/View/Helper/FormCollection view helper. This non-semantic span element contains a single data-template HTML5 attribute whose value is the whole HTML to copy to create a new element in the form. The template is indexed using the templatePlaceholder value.
Return if a template should be created.
Return type: | boolean |
---|
Set the template placeholder (defaults to __index__) used to index element in the template.
Returns the template placeholder used to index element in the template.
Return type: | string |
---|
The source code of this file is hosted on GitHub. Everyone can update and fix errors in this document with few clicks - no downloads needed.