0
votes

I am encountering a requirement where I need to use multiple hidden field elements. The values for these elements are stored in an array. What would be the best possible approach to build that in the zend form.

id[] = array('1', '2', '3', ....);

This values for id[] is dynamically generated.

And the html part for my case would look something like this

<input type="hidden" name="ids[]" value="id[0]" />
<input type="hidden" name="ids[]" value="id[1]" />
<input type="hidden" name="ids[]" value="id[2]" />

.
.
.

and so on.

How can a create multiple input hidden field elements, which I later want to use it in my controller by using the populate method.

In case if anyone has an idea how to approach this, would be really helpful.

P.S. Im using Zend Framework 2

1

1 Answers

1
votes

It sounds like you're running into a use-case anticipated by Form Collections.

\Zend\Form has baked in support for managing this kind of relational structure.

The gist of it is that you create a Fieldset which can hold a collection of identifiers for your related entity.

While the documentation doesn't demonstrate with hidden elements, there's no reason you can't create a fieldset full of hidden elements. Take a careful look at the "Category" fieldset in the documentation.