Symfony从表单事件侦听器获取控制器中的数据 [英] Symfony get data in controller from a form event listener
问题描述
我正在使用Symfony 3.4,我在preSubmit
和postSubmit
I'm using Symfony 3.4, I have a FormType
and a Listener for this form on preSubmit
and postSubmit
在我的FormType中,我像这样运行侦听器:
In my FormType I run the listener like :
$builder->addEventSubscriber(new MyListener());
我的问题是,我需要在postSubmit或preSubmit中检查获取un布尔值的条件,并且在if ($form->isSubmitted() && $form->isValid())
之后需要在控制器中使用此布尔值.
My problem is, I need in the postSubmit or preSubmit check a condition for get un boolean, and I need this boolean in my controller after the if ($form->isSubmitted() && $form->isValid())
.
我的侦听器很好,当我提交表单时,侦听器运行良好,但是我不知道如何从控制器中的侦听器获取数据.
My Listener is ok, when I submit the form the listener works well, but I don't knwo how can I get a data from the listener in my controller.
我尝试向表单添加自定义非映射字段并在Listener中设置值,但是当我尝试设置值(true | false)时出现错误:您无法更改已提交表单的值"
I tried to add a custom non mapped field to the form and set the value in the Listener, but when I try to set the value (true|false) I have an error : "You cannot change value of a submitted form"
我用setData,getData尝试过,但是..没事
I tried something with setData, getData but.. nothing
有什么想法吗?谢谢!
推荐答案
最后,我使用会话在侦听器中设置我的值,并在我的控制器中获取该值!
Finally, I use the session for set my value in the listener, and for get this value in my controller !
Tchao!
这篇关于Symfony从表单事件侦听器获取控制器中的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!