I use event listener for change data dynamically based on user inputs. Each time I use PRE_SET_DATA
and PRE_SUBMIT
events for set data and fields choices. Here is the simple example of actions from PRE_SUBMIT
:
// 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);
}
}
}
Not matter what inside addLocationField
function, it works right.
When I do $form->get('shares')
, its my collection field, then I need to ->get(child)
of this collection and set fields data and choices straight to this child. By when I add collection dynamically, Symfony shows error:
Child "n" does not exist.
And this problem happens only when I try to get data of new collection that was added dynamically. So I can't get to a collection field and change choices, so I receive error that my new value is not in a choice list.
Interesting that $data['shares']
have all data for new collection elements, but $form->get('shares')
haven`t:
var_dump(count($event->getData()['shares'])) - return 1;
var_dump(count($form->get('shares'))) - return 0;
Is that mean that my PRE_SUBMIT
works before Symfony collection functionality happen?
Someone know how to fix it?
I know your question is "old" and you probably found a solution but you were in the right direction when you said :
Your new collection is not submitted yet and it is not present in the model see this part of the doc
To make what you want to, you should use the
SUBMIT
eventNB : You can't add any field on
POST_SUBMIT