我使用事件監聽器根據用戶輸入動態更改數據。每次我使用PRE_SET_DATA
和PRE_SUBMIT
事件來設置數據和字段選項。這裏是行動從PRE_SUBMIT
簡單的例子:Symfony表單收集字段的事件監聽器
// Pre set share locations by share day
if (array_key_exists('shares', $data)) {
foreach ($data['shares'] as $key => $share) {
if ($share['pickUpDay'] !== null) {
$shareType = $form->get('shares')->get($key);
$locations = $this->em->getRepository('AppBundle:Member\Location')->getLocationsByDay($client, $data['shares'][$key]['pickUpDay']);
$this->addLocationField($shareType, $locations);
}
}
}
不是不管什麼裏面addLocationField
功能,它的作品的權利。
當我做$form->get('shares')
,它是我的收藏領域,那麼我需要->get(child)
這個集合,並將字段數據和選擇直接設置給這個孩子。通過當我動態添加收藏,Symfony的顯示錯誤:
Child "n" does not exist.
當我試圖獲取被動態地添加新的集合的數據纔會出現這種問題。因此,我無法訪問收集字段並更改選項,因此我收到錯誤消息,說明我的新值不在選擇列表中。
有趣的是$data['shares']
有新的集合元素的所有數據,但$form->get('shares')
haven`t:
var_dump(count($event->getData()['shares'])) - return 1;
var_dump(count($form->get('shares'))) - return 0;
是意味着我之前Symfony的集合功能PRE_SUBMIT
作品出現呢?
有人知道如何解決它?