Php 表单未将post值绑定到实体

Php 表单未将post值绑定到实体,php,forms,doctrine-orm,zend-framework2,zend-form,Php,Forms,Doctrine Orm,Zend Framework2,Zend Form,我有一个条令实体,一个表单和两个字段集。 当我用值填充实体时,值会像预期的那样水合到表单中。 当我尝试从表单数据创建实体时,它将保持为空 我一定忘了什么,但就是找不到。我还有其他几种没有字段集的表单,它们按预期工作 有什么想法吗 把我的代码贴在下面 实体: class User { /** * @var int * @ORM\Id * @ORM\Column(type="integer") * @ORM\GeneratedValue(strateg

我有一个条令实体,一个表单和两个字段集。 当我用值填充实体时,值会像预期的那样水合到表单中。 当我尝试从表单数据创建实体时,它将保持为空

我一定忘了什么,但就是找不到。我还有其他几种没有字段集的表单,它们按预期工作

有什么想法吗

把我的代码贴在下面

实体:

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;

    ..
}
表格:

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

    }
}
用户字段集:

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',
                ),
            )
        );
        ...
    }
}
基线集:

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

    }
}
控制器操作:

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

$form->isValid()
之后,是否可以使用var_dump
$form->getData()


或者,您可以尝试
$form->isValid($request->getPost())
而不是
setData()

嗨,$form-getData()的变量转储显示一个“空”用户实体(空表示值都为null)。如果我使用setData或不使用,..$form->isValid($request->getPost())没有区别不起作用,因为它只是说我的表单对setData无效,但它确实验证了用户实体值为nullimho,$form->isValid($request->getPost())起作用,但验证有问题。您可以尝试var_dump$request->getPost()。您是否在表单上调用了
prepare()
,以正确启动该结构?$request->getPost()dump显示了Zend\Stdlib\Parameters的对象,其中有两个属性“user”和“base”,它们都是包含表单字段名=>值的数组。所以这是意料之中的事,对吗?我在视图中调用prepare()。我不能用$form->isValid($request->getPost())在isValid()之前调用setData()来验证我的表单。它会验证,如果用户是基本字段集呢<代码>$userFieldset->setUseAsBaseFieldset(真)而不是baseFieldset