form not binding post values to entity

2019-07-13 11:26发布

I have a doctrine entity, a form and 2 fieldsets. When i populate the entity with values the values get hydrated into the form as expected. When i try to create a entity from form data it stays empty..

i must be forgetting something but just cant find it, i have several other forms without fieldsets and they work as expected.

any ideas?

posted my code below

Entity:

class User
{
    /**
     * @var int
     * @ORM\Id
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;

    /**
     * @var string
     * @ORM\Column(type="string", length=255, unique=true, nullable=true)
     */
    protected $username;

    ..
}

Form:

class CreateUserForm extends Form
{
    public function __construct(ObjectManager $objectManager)
    {
        parent::__construct('create-user');

        $this->setAttribute('method', 'post');

        // The form will hydrate an object 
        $this->setHydrator(new DoctrineHydrator($objectManager));

        $userFieldset = new UserFieldset($objectManager);
        $this->add($userFieldset);

        // … add CSRF and submit elements …
        $baseFieldset = new BaseFieldset($objectManager);
        $baseFieldset->setUseAsBaseFieldset(true);
        $this->add($baseFieldset);

    }
}

UserFIeldset:

class UserFieldset extends Fieldset implements InputFilterProviderInterface
{
    public function __construct($objectManager)
    {
        parent::__construct($name = 'user');

        $this->setHydrator(
            new DoctrineHydrator($objectManager, 'YrmUser\Entity\User')
        )->setObject(new User());     



        $this->add(
            array(
                'name' => 'username',
                'attributes' => array(
                    'type'  => 'text',
                    'placeholder' =>'Username',
                ),
                'options' => array(
                    'label' => 'Username',
                ),
            )
        );
        ...
    }
}

BaseFieldset:

class BaseFieldset extends Fieldset implements InputFilterProviderInterface
{
    public function __construct($objectManager)
    {
        parent::__construct('base');
        $this->setHydrator(new DoctrineHydrator($objectManager));
        $this->add(
            array(
                'name' => 'security',
                'type' => 'Zend\Form\Element\Csrf',
                'options' => array(
                    'csrf_options' => array(
                        'timeout' => 600
                    )
                )
            )
        );

        $this->add(
            array(
                'name' => 'submit',
                'attributes' => array(
                    'type'  => 'submit',
                    'value' => 'Save',
                    'class' => 'btn btn-success btn-lg confirm',
                ),
            )
        );

    }
}

controller action:

public function createAction()
{

    $form = new CreateUserForm($this->getObjectManager());
    $entity = new User();
    $form->bind($entity);

    $request = $this->getRequest();
    if ($request->isPost()) {
        $form->setData($request->getPost());
        if ($form->isValid()) {
            $this->getObjectManager()->persist($entity);
            $this->getObjectManager()->flush();
            return $this->redirect()->toRoute($this->redirect);
        }
    }

    return array(
        'form' => $form
    );
}

1条回答
时光不老,我们不散
2楼-- · 2019-07-13 11:37

Can you var_dump $form->getData() after $form->isValid() ?

Alternatively, you can try $form->isValid($request->getPost()) instead of setData().

查看更多
登录 后发表回答