簡體   English   中英

帶有字段集和原則的ZF2表單不起作用

[英]ZF2 form with fieldset and doctrine not working

我對表格,字段集和學說有疑問。 這是我的編輯表格。 在此表單中,我添加了用戶字段集,並添加了另一個字段“密碼” (僅在此表單中使用)。

EditUserForm:

class EditUserForm extends Form implements InputFilterProviderInterface
{
    public function __construct($name = null, $options = [])
    {
        parent::__construct($name, $options);

        $this->setAttribute('method', 'post');
        $this->setHydrator(new ClassMethods(false));
        $this->setObject(new User());

        $this->add([
            'name'    => 'user',
            'type'    => 'Application\Form\UserFieldset',
            'options' => [
                'use_as_base_fieldset' => true
            ]
        ]);

        $this->add([
            'name'       => 'password',
            'type'       => 'Zend\Form\Element\Password',
            'attributes' => [
                'id'    => 'password'
            ]
        ]);
    }

    public function getInputFilterSpecification()
    {
        return [
            'password' => [
                'required' => true
            ],
        ];
    }

}

UserFieldset:

class UserFieldset extends Fieldset implements InputFilterProviderInterface
{
    public function __construct($name = null, $options = [])
    {
        parent::__construct($name, $options);

        $this->setHydrator(new ClassMethods(false));
        $this->setObject(new User());

        $this->add([
            'name'       => 'name',
            'type'       => 'Zend\Form\Element\Text',
            'attributes' => [
                'id'    => 'name'
            ]
        ]);

        $this->add([
            'name'       => 'surname',
            'type'       => 'Zend\Form\Element\Text',
            'attributes' => [
                'id'    => 'surname'
            ]
        ]);
    }

    public function getInputFilterSpecification()
    {
        return [
            'name' => [
                'required' => true
            ],
            'surname' => [
                'required' => true
            ],
        ];
    }
}

為什么如果我嘗試使用var_dump(form->getData()) ,該實體沒有密碼字段?

object(Application\Entity\User)[114]
  private 'name' => string 'john' (length=4)
  private 'surname' => string 'smith' (length=5)
  private 'password' => null

謝謝。

密碼需要是部分UserFieldset為你設置UserFieldset作為基礎字段集。 如果選擇一個基本字段集,則僅此字段集將被遞歸合並。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM