Skip to content
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

[Form][3.0] setData in FormEvents::SUBMIT doesn't work in compound forms #18763

Closed
tobske opened this issue May 12, 2016 · 3 comments
Closed

Comments

@tobske
Copy link

tobske commented May 12, 2016

If you have a compound form and you call setData() with changed data inside a listener for the FormEvents::SUBMIT the data is not changed correclty. When the handleRequest() function is finished the normdata, viewdata and modeldata of the compound form are correct but the values of the children forms haven't changed. If you call $form->createView() after this the form doesn't contain the changes.

Example code to reproduce bug:
Controller:

public function newAction(Request $request)
    {
        $form = $this->createForm(FormName::class);
        $form->handleRequest($request);
        return $this->render(
            "AppBundle:Default:form.html.twig",
            [
                'form' => $form->createView()
            ]
        );
    }

Form:

class FeatureType extends AbstractType implements EventSubscriberInterface
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('name')
            ->addEventSubscriber($this)
        ;
    }

    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults(['data_class' => 'Cat\AppBundle\PlainClasses\Form\FeatureDTO']);
    }

    public static function getSubscribedEvents()
    {
        return [
            FormEvents::SUBMIT => 'submit'
        ];
    }

    public function submit(FormEvent $event)
    {
        $data = $event->getData();
        $data->name = 'test';
        $event->setData($data);
    }
}

DTO:

class FeatureDTO
{
    /**
     * @var string
     */
    public $name;
}

With this code you will see that the normdata, viewdata and modeldata of the complete form featureType is updated and the name is set to 'test' but the child form, the name form that is a TextType, still has an empty value so if you render the form the name field stays empty.

@tobske
Copy link
Author

tobske commented May 13, 2016

To sum it up: The data change by using setData does change the data but this changed isn't propagated to the children. So by rendering the form after this the form fields still have the old values.

@mccullagh
Copy link
Contributor

I can reproduce it, but its not a bug. Its a bit complicated to explain lets say the children get submitted before.

you have two option to solve it:

  • Add the Event to the sub type like
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('name')
        ;

        $builder
            ->get('name')
            ->addEventSubscriber($this);
    }
  • Use the PRE_SUBMIT from the form events

I hope this helps you

@tobske
Copy link
Author

tobske commented May 25, 2016

I already expected that the submit of the children has been handled before. I can't use the first option because I want to change a different field (e.g. 'age') that is on the same level as 'name'. So if I handle a event on name I can't change the data of another field then name, AFAIK.
I will then have to go for the second options. Then I have to resolve some entities myself which would have been resolved automatically if I could use the SUBMIT event.

IMHO it would be very helpful to add a extended workflow with child forms to the Form Events page in the cookbook (http://symfony.com/doc/current/components/form/form_events.html). This would help to undestand when which event on which form (parent or child) gets triggerd.

@tobske tobske closed this as completed May 25, 2016
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