Form ZF2: Ordering of Elements

I am creating a form for a logged in user to change my password, so I subclassed the existing password-reset form I have. The forms will be identical, except for an additional field for the existing password. It has worked so far, except that I cannot figure out a way to manually set the order in the new field; the only place I got is the end of the form. It seems like ZF2 requires you to have form elements add()

in the order you want to render them. I would do this, except that the subclass form constructor must have a parent form constructor before it can add new fields, at which point the parent form has already added its fields.

I already tried to set a property on order

my new field, but it didn't work; I have tried several different combinations (I cannot find any documentation for this function anywhere, after a lot of searching).

Snippet of the subclass constructor:

class ChangePassword extends ResetPassword implements InputFilterProviderInterface {

public function __construct() {
    parent::__construct();

    $this->add(array(
        'type' => 'Zend\Form\Element\Password',
        'name' => 'existingPassword',
        'order' => 0,
        'options' => array(
            'label' => 'Existing Password',
            'order' => 0,
        ),
        'attributes' => array(
            'required' => 'required',
            'order' => 0,
        )
    ));
}

      

Parent constructor snippet:

class ResetPassword extends Form implements InputFilterProviderInterface {

    public function __construct() {
        parent::__construct('reset-password');

        $this->add(array(
            'type' => 'Zend\Form\Element\Password',
            'name' => 'password',
            ...

      

+3


source to share


2 answers


The key you are looking for that affects the order of the items is called priority.

The method add()

accepts a second containing array, $flags

and it must add a priority

key / value pair in this array .



Your constructor should look something like this:

class ChangePassword extends ResetPassword implements InputFilterProviderInterface {

    public function __construct() {
        parent::__construct();

        $this->add(array(
            'type' => 'Zend\Form\Element\Password',
            'name' => 'existingPassword',
            'options' => array(
                'label' => 'Existing Password',
            ),
            'attributes' => array(
                'required' => 'required',
            )
        ), // add flags array containing priority key/value 
        array(
            'priority' => 1000, // Increase value to move to top of form
        ));
    }
}

      

+6


source


I have a problem today, Crisp's answer helped, but I think it would be good to clarify this:

In the view, we have many options to show our form:

  • <?= $this->form($form)?>

  • <?= $form->get('{element}') ?>

  • loop over $form->getIterator()

  • loop over $form->getElements()

etc...



I must say that I have used this structure in all my projects:

<?php foreach ($form->get('fieldset')->getElements()  as $elementName => $element): ?>
     <?= $this->partial('partial/formElement', ['element' => $element])?>
<?php endforeach ?>

      

The problem is that getElements doesn't take precedence, so it just gives the element in order when it was created.

In the view, we have to use the iteration ( $form->getIterator()

) method to return this priority.

0


source







All Articles