Scroll the form elements in the order in which they were added.

Zend Form 2 structures all the elements in the Fieldsets. (Zend \ Form \ Form extends Fieldset - Form :: add calls parent :: add)

If I just add Elements to the form, I can get them through $form->getElements(), if I use a set of fields, I can get them through

foreach($form->getFieldsets() as $fieldset){
  $elements = $fieldset->getElements();
}

But imagine a form in which I add some hidden fields, then a set of fields, and finally a submit button.

How can I get the elements / fields in the correct order?

The reason for this is that I am working on a view helper that allows me to print forms through a simple call to the view helper.

I don’t want to call every element of the form on call formRow() (I know the concept of Form2 - separating logic from presentation)

Any help is greatly appreciated. TIA

+5
1

, , :

/* $form is an instance of \Zend\Form\Form */
foreach ($form as $element) {

    // check if it a form element or a fieldset etc.
    // and recursively iterate over elements of fieldsets etc.
}
+9

All Articles