javascript Symfony 2 根据所选值动态更改表单
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10932142/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Symfony 2 dynamiclly change form based on selected value
提问by Ruben Nagoga
I have the following table:
我有下表:
Events:
- Date (date)
- EventType (varchar)
- Value (varchar)
EventType can have a few values i.e checkin, checkout, roomrent, misc etc. I want to make form which will change field type of "Value" to text,date or entity based on selected type in "EventType". I tried to find some solutions but didn't succeed. The only thing I foundthat I have to use queryBuilder but really can catch how to do it.
EventType 可以有几个值,即签入、签出、roomrent、misc 等。我想制作表单,根据“EventType”中的选定类型将“值”的字段类型更改为文本、日期或实体。我试图找到一些解决方案,但没有成功。我发现我必须使用 queryBuilder的唯一一件事,但真的可以了解如何去做。
回答by Ahmed Siouani
You should use Event Subscribers, by adding an event subscriber to your Form, you delegate the creation of your fields to that Subscriber. Inside your Event Subscriber your could check your "EventType" and add the appropriate "Value" field.
您应该使用事件订阅者,通过向您的表单添加事件订阅者,您将字段的创建委托给该订阅者。在您的事件订阅者中,您可以检查您的“EventType”并添加适当的“Value”字段。
回答by Ruben Nagoga
I've found solution. I added EventSubscriber
to my form and send ajax request with type which user has selected to let subscriber decide what kind of fields add.
我找到了解决方案。我添加EventSubscriber
到我的表单并发送带有用户选择的类型的ajax请求,让订阅者决定添加什么样的字段。
$subscriber = new EventSubscriber($builder->getFormFactory());
$builder->addEventSubscriber($subscriber);
$builder->add('eventType', 'entity', array('class' => 'Bundle:EventType', 'property' => 'eventName', 'required' => true));
Thanks to Ahmed Siouani.
感谢艾哈迈德·西瓦尼。