本文介绍了形式没有绑定后值到实体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个学说实体,一个表单和两个字段。
当我使用值填充实体时,值将按预期水合到表单中。
当我尝试从表单数据创建一个实体时,它保持空白。



我必须忘记一些东西,但是找不到它,我有几个其他的表单没有fieldets,他们按预期工作。



任何想法?



发布我的代码下面



实体:

 类用户
{
/ **
* @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)
{
父:: __构建体(创建用户);

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

//表单将水合一个对象
$ this-> setHydrator(new DoctrineHydrator($ objectManager));

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

// ...添加CSRF并提交元素...
$ 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' =>数组(
'type'=>'text',
'占位符'=>'用户名',
),
'options'=& (
'label'=>'用户名',
),

);
...
}
}

BaseFieldset: p>

  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'=>数组(
'csrf_options'=>数组(
'timeout'=> 600



);

$ this-> add(
array(
'name'=>'submit',
'attributes'=>数组(
'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);
}
}

返回数组(
'form'=> $ form
);
}


解决方案

你可以var_dump $ form-> getData() after $ form-> isValid()



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


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
    );
}
解决方案

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

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

这篇关于形式没有绑定后值到实体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 22:05