Skip to content

[Form] Event form modification improvements #9254

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

Merged

Conversation

julienfalque
Copy link
Contributor

  • FormEvents::SUBMIT does allow to modify the form (tested on Symfony 3.3 but looking at the code, this seems to be true for 2.7 as well);
  • FormEvents::POST_SUBMIT allows to modify the parent form, this was not explicit in the documentation so I thought it would be a nice addition.

@javiereguiluz
Copy link
Member

@vudaltsov you know Symfony Forms very well. If you have some time, could you please review this proposal? Thanks a lot!


The ``FormEvents::POST_SUBMIT`` event does not allow to modify the form
the listener is bound to, but it allows to modify its parent.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure I understand your comment, what can we add here exactly?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just agree with your proposal! Let's add what you suggest :)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Although it supports the example, I fail to see the logic behind it. Why does it work that way? It looks like a loophole (failure to verify that a form field is added when it's made from a children) that is used in a very common example.

form/events.rst Outdated
.. caution::

At this point, you cannot add or remove fields to the form.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was already discussed in symfony/symfony#20770
It doesn't make sense to change data on SUBMIT and POST_SUBMIT.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This notice is not about changing the data but the form fields. Trying to do so only results in an exception being thrown on POST_SUBMIT event, not on SUBMIT event. Maybe an exception should be thrown on SUBMIT event too?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reading related discussions, it seems that SUBMIT event allowing to change form fields is intentional though it is suitable for a few edge cases only. What about updating the notice to say exactly that, rather than completely removing it?

@@ -219,7 +215,8 @@ View data Normalized data transformed using a view transformer

.. caution::

At this point, you cannot add or remove fields to the form.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here I just make explicit that child fields cannot be changed on POST_SUBMIT, is that wrong?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, misread the changes. This is correct.

@vudaltsov
Copy link
Contributor

vudaltsov commented Feb 26, 2018

Another related PR: #5996

@javiereguiluz
Copy link
Member

@julienfalque do you think that your pull request is ready to merge? Thanks!

@julienfalque
Copy link
Contributor Author

@javiereguiluz I would like to have some feedback to #9254 (comment) before merging.

@vudaltsov
Copy link
Contributor

I will give that feedback today, I will try to use those events the way you explain and we'll discuss it.
As for the other two changes, I am 👍

@vudaltsov
Copy link
Contributor

I tried to add a field on a SUBMIT event to the current form and although it didn't throw any exceptions and the field appeared in the view, the data was not handled and did not appear in the result array.

$builder = $this->createFormBuilder()
    ->add('children', FormType::class)
    ->add('submit', SubmitType::class, [
        'attr' => ['class' => 'btn btn-black']
    ]);

$builder
    ->get('children')
    ->add('text', TextType::class)
    ->addEventListener(FormEvents::SUBMIT, function (FormEvent $event) {
        $event->getForm()->add('textarea', TextareaType::class);
    });

$form = $builder
    ->getForm()
    ->handleRequest($request);

image

So I think the notice is correct and should not be removed.

@vudaltsov
Copy link
Contributor

I noticed that ResizeFormListener actually removes fields from the listened form on SUBMIT but I don't think this possibility is worth mentioning in the docs.

@julienfalque julienfalque force-pushed the form-events-modifiable-forms branch from 5ecc632 to d40285b Compare March 9, 2018 18:34
@julienfalque
Copy link
Contributor Author

Ok, I reverted the notice removal.

@javiereguiluz
Copy link
Member

@julienfalque thanks for this contribution ... and thanks @vudaltsov for your great review. Cheers!

@javiereguiluz javiereguiluz merged commit d40285b into symfony:2.7 Mar 18, 2018
javiereguiluz added a commit that referenced this pull request Mar 18, 2018
This PR was merged into the 2.7 branch.

Discussion
----------

[Form] Event form modification improvements

* `FormEvents::SUBMIT` *does* allow to modify the form (tested on Symfony 3.3 but looking at the code, this seems to be true for 2.7 as well);
* `FormEvents::POST_SUBMIT` allows to modify the parent form, this was not explicit in the documentation so I thought it would be a nice addition.

Commits
-------

d40285b Event form modifications improvements
@julienfalque julienfalque deleted the form-events-modifiable-forms branch March 19, 2018 00:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants