Judging by these questions:
Zend framework 2 loop through elements of element collection
I suppose there isn't a good way to customize the elements of a collection.
As example, having a collection like:
//path: MyModule\Form\MyFieldset
public function __construct($name = null) {
parent::__construct('myFieldset');
$this->add([
'name'=>'test',
'type' => Element\Collection::class,
'options' => [
'label' => 'MyCollection',
'count' => 6,
'should_create_template' => true,
'target_element' => new Element\Text()
],
]);
}
Then do something in order to define (here, into the current class) custom attributes for each text element and/or autonumbered labels and then output (simply calling the zend helper FormCollection, without any custom view helper):
<label>
<span>text element n° 1</span>
<input type="text" name="myFielset[test][0]" id='myId_0' alt='input 0' value="">
</label>
<label>
<span>text element n° 2</span>
<input type="text" name="myFielset[test][1]" id='myId_1' alt='input 1' value="">
</label>
[...]
Am I wrong?
(I'm asking that because I've found a nice solution to do that and maybe could be helpful to post it)
target_element
must reference a fieldset. This can be either a new instance within the form where the collection is, or a class name.For example:
or
In terms of customising the label for each input, I have not yet found a way to do that.
Not too sure of the inner workings of how the collection works, but I suspect it creates as many new instances of
target_element
as required. In terms of just adding a number to the label (or arbitrary attribute) you could do a fieldset class with a static property that starts with1
, add it to your label and increment its value?For example: