-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[3.0] [Form] Map data to parent form data before POST_SUBMIT #8737
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
The question here is when to invoke the closure in the "empty_data" option, which is used now to pass field values to the constructor of the entity. Right now, the closure is invoked after submitting the fields, but before mapping them to the data:
The solution here would be to submit unmapped fields first:
|
Another side effect of this workflow is that the initial value of the added field is never applied on add. The workaround is to manually set empty_data and return the field's viewData. Here's the revised version of the example. $updateOnlinePayment = function (FormEvent $event) {
$form = $event->getForm()->getParent();
$order = $form->getData();
$form->add('paymentMethod', 'choice', array(
'disabled' => !$order->isOnlinePaymentSupported(),
// as of 2.3
'auto_initialize' => false
// if parent form data has default value, this field will be empty if this option is not set.
// datatransformer will also take effect
'empty_data' => function($form) {
return $form->getViewData();
},
));
}
$builder->get('country')->addEventListener(FormEvents::POST_SET_DATA, $updateOnlinePayment);
$builder->get('country')->addEventListener(FormEvents::POST_SUBMIT, $updateOnlinePayment); |
This may be achievable by split the form tree update and the deserialization (array -> object graph) as suggested in #6399 and others. |
For me this is more usefull and intuitive than #5807. |
I am closing this old issue as it didn't get any traction in years and it doesn't seem to be implemented in the future. |
Right now when submitting a form, all child forms are first submitted, then the data of these child forms is mapped back into the parent data by the parent form.
Shifting the mapping logic into the child form before POST_SUBMIT would make it possible to access changed state in the parent data in POST_SUBMIT listeners. For example:
Related to #5807.
The text was updated successfully, but these errors were encountered: