Symfony:我可以从Type / Form返回null吗?
16lz
2021-01-22
I have an entity form with Symfony :
我有一个Symfony的实体表格:
class MyType extends AbstractType
{
/**
* @param FormBuilderInterface $builder
* @param array $options
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
...
}
/**
* @param OptionsResolverInterface $resolver
*/
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'LogicielBundle\Entity\FichierGroup',
'intention' => $this->getName() . '_token'
));
}
But in POST_SUBMIT event, I want to return null (no entity). I tested this but not working :
但是在POST_SUBMIT事件中,我想返回null(没有实体)。我测试了这个,但没有工作:
$builder->addEventListener(FormEvents::POST_SUBMIT, function(FormEvent $event) {
.... my condition ...
$event->setData(null);
});
Can you help me ? Thanks :)
你可以帮我吗 ?谢谢 :)
3 个解决方案
#1
1
Could you please post you controller code? Do you pass an object reference to createForm
, or do you use $form->getData()
? In your case, you should stick to the second.
你能告诉你控制器代码吗?您是否将对象引用传递给createForm,还是使用$ form-> getData()?在你的情况下,你应该坚持第二个。
Try using SUBMIT event instead of POST_SUBMIT. As Symfony doc states it, "It can be used to change data from the normalized representation of the data.".
尝试使用SUBMIT事件而不是POST_SUBMIT。正如Symfony doc所述,“它可用于从数据的规范化表示中更改数据。”
更多相关文章
- 使用/发送POST数据时的libcurl C问题(不是c++)
- PHP:在类中使用数据库
- 纯真ip数据库查询的php实现(补充分组查询)
- 添加到数据库后,保持在同一页面而不刷新它
- PHP 导出 万级别数据 时间测试
- 数据库是存储我的Web应用程序数据的最佳选择吗?
- 使用没有实体类的本机SQL查询
- php执行数据库查询返回json格式数据
- PHP更新基于其他表的表数据