I've written a symfony FormType
-Class which has a POST_SUBMIT
EventListener, in which I wanted to set a default value for an
existing field dependant on another, filled form element.
In other words: dependant on a selected, existing user (entity select) I want to set the default data of another entity-select field (country and state) to the correspondant value.
The code looks like this:
$formModifier = function (FormInterface $form, Person $person = null)
{
if ($person) {
$form->get('country')->setData(
$person->getCountry()
);
}
};
By selecting the user the form is submitted but not successfully validated, so the Country should be selected. The Person-Object exists, the value is correct and the statement is reached.
But the value isn't selected. Isn't this possible by using a POST_SUBMIT
-FormEvent?
Additional info: symfony 2.7, PHP 5.6, implementation done like the second example under http://symfony.com/doc/2.7/cookbook/form/dynamic_form_modification.html#dynamic-generation-for-submitted-forms
Thanks in advance!
Martin
Edit:
The formModifier
is called like this:
$builder->get('field')->addEventListener(
FormEvents::POST_SUBMIT,
function (FormEvent $event) use ($formModifier) {
$person = $event->getForm()->getData();
$formModifier($event->getForm()->getParent(), $person)
}
};