ZF2 表单:自定义元素顺序 [英] ZF2 Form: Customizing order of elements

查看:23
本文介绍了ZF2 表单:自定义元素顺序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为登录用户创建一个表单来更改他们的密码,因此我创建了一个可用的现有密码重置表单的子类.除了现有密码的附加字段外,表格将相同.到目前为止,它已经奏效了,只是我想不出一种方法来手动设置新字段的顺序;我让它出现的唯一地方是在表格的末尾.ZF2 似乎要求您按照您希望元素呈现的顺序 add() 形成元素.我会这样做,除了子类表单的构造函数必须是父表单的构造函数才能添加新字段,此时父表单已经添加了它的字段.

I'm creating a form for a logged-in user to change their password, so I created a subclass of an existing password-reset form I have available. The forms will be identical except with an additional field for existing password. It's worked so far, except I can't figure out a way to manually set the order the new field; the only place I've gotten it to appear is at the end of the form. It seems that ZF2 requires you to add() form elements in the order that you want them rendered. I would do so, except the subclass form's constructor must the parent form's constructor before it can add new fields, by which point the parent form has already added its fields.

我已经尝试设置我的新字段的属性 order,但是没有用;我尝试了几种不同的组合(经过大量搜索,我在任何地方都找不到此功能的文档).

I have already tried setting the property order of my new field, but it did not work; I've tried several different combinations (I can't find the documentation for this feature anywhere, after lots of searching).

子类构造函数片段:

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,
        )
    ));
}

父构造函数片段:

class ResetPassword extends Form implements InputFilterProviderInterface {

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

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

推荐答案

您正在寻找的影响元素顺序的键名为优先级.

The key you're looking for which affects element order is named priority.

表单 add() 方法接受包含 $flags 的第二个数组,并且您必须在该数组中添加 priority 键/值对.

The form add() method accepts a second array containing $flags, and it's in this array that you must add the priority key/value pair.

你的构造函数最终应该看起来像这样......

Your constructor should end up looking 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
        ));
    }
}

这篇关于ZF2 表单:自定义元素顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆