gpt4 book ai didi

php - 表单未将帖子值绑定(bind)到实体

转载 作者:搜寻专家 更新时间:2023-10-31 21:35:16 25 4
gpt4 key购买 nike

我有一个学说实体、一个表单和 2 个字段集。当我用值填充实体时,这些值会按预期混合到表单中。当我尝试从表单数据创建实体时,它保持为空。

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

有什么想法吗?

在下面贴出我的代码

实体:

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

}
}

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

最佳答案

你能在 $form->isValid() 之后 var_dump $form->getData() 吗?

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

关于php - 表单未将帖子值绑定(bind)到实体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21623724/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com