Skip to content

[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

Closed
webmozart opened this issue Aug 13, 2013 · 5 comments
Closed

[3.0] [Form] Map data to parent form data before POST_SUBMIT #8737

webmozart opened this issue Aug 13, 2013 · 5 comments

Comments

@webmozart
Copy link
Contributor

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:

$updateOnlinePayment = function (FormEvent $event) {
    $form = $event->getForm()->getParent();
    $order = $form->getData();

    $form->add('paymentMethod', 'choice', array(
        'disabled' => !$order->isOnlinePaymentSupported(),
    ));
}

$builder->get('country')->addEventListener(FormEvents::POST_SET_DATA, $updateOnlinePayment);
$builder->get('country')->addEventListener(FormEvents::POST_SUBMIT, $updateOnlinePayment);

Related to #5807.

@webmozart
Copy link
Contributor Author

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:

  1. Submit fields
  2. Invoke "empty_data" closure to create entity
  3. Map fields into entity

The solution here would be to submit unmapped fields first:

  1. Submit unmapped fields
  2. Invoke "empty_data" closure, may access data of unmapped fields
  3. Submit mapped fields (maps into entity automatically)

@marfillaster
Copy link
Contributor

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);

@webmozart
Copy link
Contributor Author

This may be achievable by split the form tree update and the deserialization (array -> object graph) as suggested in #6399 and others.

@webmozart webmozart changed the title [Form] Map data to parent form data before POST_SUBMIT [3.0] [Form] Map data to parent form data before POST_SUBMIT Oct 17, 2014
@raziel057
Copy link
Contributor

For me this is more usefull and intuitive than #5807.

@xabbuh
Copy link
Member

xabbuh commented Jul 23, 2019

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.

@xabbuh xabbuh closed this as completed Jul 23, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants