我有一个带有绑定(bind)选择的表单,即更改 select 1 可能会更新 select 2,而 select 2 可能会更新 select 3 等等。
为了给我的选择提供正确的“选择”(只有少数应该在加载时显示,另一个在更改选择后显示。在 POST 之后,还应该设置正确的选择),我设置了一个事件监听器在 PRE_SET_DATA 事件上。
在这个监听器中,我有:
$updatetodate_object = $event->getData();
$form = $event->getForm();
问题是我的 $updatetodate_object 不是最新的,在 POST 之后仍然有来自数据库的值而不是用户的数据,而我需要更新的值来构建“选择”。
我也试过:
$form->getData()
返回异常:
A cycle was detected. Listeners to the PRE_SET_DATA event must not call setData().
You should call setData() on the FormEvent object instead.
有什么想法吗?
最佳答案
However, things get more difficult when you handle the form submission. This is because the PRE_SET_DATA event tells us the data that you're starting with (e.g. an empty SportMeetup object), not the submitted data.
你应该尝试另一个事件。 PRE_SUBMIT 是您可以获得提交数据的第一个位置。
关于forms - Symfony2 : get post data from PRE_SET_DATA event,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28856261/