zend-framework2zend-form-elementzend-form2

How to add zend form element at specific position?


I have created an abstract form class that adds a submit element to the form. When my other form classes extend this form class I am having to call parent::__construct() at the bottom of the class so that the submit element is added last.

How do I set the submit button to always be the last element in the form, event if I call parent::__construct() first?


Solution

  • The form add function accepts two parameters - either an array or element instance, and an array of flags. One of the flag options is priority. The smaller the number, the lower in the list of elements it will appear. Therefore, setting it to -1000 (unless you have greater than 1000 elements) will set the submit button to appear at the bottom of the form:

    $this->add([
        'name' => 'submit',
        'type' => 'submit',
        'attributes' => [
            'value' => 'Submit',
            'class' => 'btn btn-sm',
        ],
        'options' => []
    ],
    [ 'priority' => -1000 ]);