ZF2形式与fieldset和doctrine不起作用

时间:2015-06-11 14:08:33

标签: php doctrine-orm zend-framework2 doctrine zend-form

我遇到表单,字段集和学说的问题。 这是我的编辑表格。在此表单中,我添加了用户字段集并添加了另一个字段"密码" (我只在此表单中使用)。

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

感谢。

1 个答案:

答案 0 :(得分:0)

当您将UserFieldset设置为base-fieldset时,密码必须是UserFieldset的一部分。如果选择基本字段集,则只有该字段集将以递归方式进行水合。