[英]How to add zend form element at specific position?
I have created an abstract form class that adds a submit
element to the form. 我创建了一个抽象表单类,它将一个submit
元素添加到表单中。 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. 当我的其他表单类扩展此表单类时,我不得不在类的底部调用parent::__construct()
,以便最后添加submit元素。
How do I set the submit button to always be the last element in the form, event if I call parent::__construct()
first? 如果我首先调用parent::__construct()
,如何将提交按钮设置为始终是表单中的最后一个元素?
The form add
function accepts two parameters - either an array or element instance, and an array of flags
. 表单add
函数接受两个参数 - 数组或元素实例以及flags
数组。 One of the flag options is priority
. 其中一个标志选项是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: 因此,将其设置为-1000(除非您有超过1000个元素)将设置提交按钮显示在窗体的底部:
$this->add([
'name' => 'submit',
'type' => 'submit',
'attributes' => [
'value' => 'Submit',
'class' => 'btn btn-sm',
],
'options' => []
],
[ 'priority' => -1000 ]);
RichardParanby-King's answer is good but careful for the view : 理查德·帕兰比 - 金的回答很好,但对于观点是谨慎的:
In the view we have a lot of options to show the form : 在视图中,我们有很多选项来显示表单:
<?= $this->form($form)?>
<?= $form->get('{element}') ?>
$form->getIterator()
循环遍历$form->getIterator()
$form->getElements()
循环$form->getElements()
etc... 等等...
I have to say I used a lot this structure in all of my projects : 我不得不说我在所有项目中都使用了很多这个结构:
<?php foreach ($form->get('fieldset')->getElements() as $elementName => $element): ?>
<?= $this->partial('partial/formElement', ['element' => $element])?>
<?php endforeach ?>
The problem is : getElements does not use priority, so its just give the element in order of when it was instanciated. 问题是:getElements不使用优先级,因此它只是按照实例化的顺序给出元素。
In the view we have to use the iteration method ( $form->getIterator()
) to get back this priority. 在视图中,我们必须使用迭代方法( $form->getIterator()
)来获取此优先级。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.