ZF2 form with fieldset and doctrine not working

142 views Asked by At

I have a problem with a form, fieldset and doctrine. This is my edit form. In this form I add the User Fieldset and add another field "password" (that I use only in this 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
            ],
        ];
    }
}

Why if I try to var_dump(form->getData()) does the entity does not have the password field?

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

thanks.

1

There are 1 answers

1
Sam On

The password needs to be part of the UserFieldset as you're setting the UserFieldset as base-fieldset. If you choose a base-fieldset, only this fieldset will be hydrated recursively.