如何在提交到数据库之前修改表单请求的值?
How to modify value from a form request before it is submitted in the DB?
我使用事件监听器检查 user/author 返回的一些值,然后再实际将它们提交给数据库。
我的表单允许 ROLE_USERS 更改实体文章的某些属性(如 statut)。文章具有 statut(Published, Delete) 和 tobecheked('author wants to delete this article', 'null') 等属性,允许 ROLE_ADMIN 轻松查看某些文章。
无论如何。我希望当作者提交 statut=>'DELETE' statut tobechecked 的值变成 => 'author wants to delete this article' .
这是我尝试过的方法,但这不起作用。
class Article extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('Statut', ...);
->add('toBeChecked', ...);
$dynamicSubmissionFunc = function (FormEvent $event)
{
$data = $event->getData();
if($data['statut'] == 'DELETE')
{
// HOW CAN I MODIFY HERE THE VALUE OF ARTICLE ATTRIBUTE
$data['toBeChecked'] = 'author wants to delete this article';
// nothing is changed in the attribute toBeChecked.
}
}
$builder->addEventListener(FormEvents::PRE_SUBMIT, $dynamicSubmissionFunc);
}
}
好的。我想通了。您需要将数据设置回事件:
$event->setData($data);
我使用事件监听器检查 user/author 返回的一些值,然后再实际将它们提交给数据库。
我的表单允许 ROLE_USERS 更改实体文章的某些属性(如 statut)。文章具有 statut(Published, Delete) 和 tobecheked('author wants to delete this article', 'null') 等属性,允许 ROLE_ADMIN 轻松查看某些文章。
无论如何。我希望当作者提交 statut=>'DELETE' statut tobechecked 的值变成 => 'author wants to delete this article' .
这是我尝试过的方法,但这不起作用。
class Article extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('Statut', ...);
->add('toBeChecked', ...);
$dynamicSubmissionFunc = function (FormEvent $event)
{
$data = $event->getData();
if($data['statut'] == 'DELETE')
{
// HOW CAN I MODIFY HERE THE VALUE OF ARTICLE ATTRIBUTE
$data['toBeChecked'] = 'author wants to delete this article';
// nothing is changed in the attribute toBeChecked.
}
}
$builder->addEventListener(FormEvents::PRE_SUBMIT, $dynamicSubmissionFunc);
}
}
好的。我想通了。您需要将数据设置回事件:
$event->setData($data);