douyan2970 2014-02-07 09:19
浏览 52
已采纳

表单不将post值绑定到实体

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条回答 默认 最新

  • douxieshang5577 2014-02-07 10:58
    关注

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

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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?
  • ¥15 matlab(相关搜索:紧聚焦)
  • ¥15 基于51单片机的厨房煤气泄露检测报警系统设计
  • ¥15 Arduino无法同时连接多个hx711模块,如何解决?