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] Adding valid & invalid form events #47046

Open
Seb33300 opened this issue Jul 24, 2022 · 5 comments · May be fixed by #47210
Open

[Form] Adding valid & invalid form events #47046

Seb33300 opened this issue Jul 24, 2022 · 5 comments · May be fixed by #47210

Comments

@Seb33300
Copy link
Contributor

Seb33300 commented Jul 24, 2022

Description

This issue is not a bug but more a discussion about adding new events to the form component.

I am thinking about 2 new events that could occurs after the form.post_submit submit event.

  1. form.valid when the entire submitted form is valid
  2. form.invalid when at least one item of the submitted form failed to be valid

Those events will be useful in cases where we need to check that the form passed validation.
Using $form->isValid() in the form.post_submit event only checks that the current form item is valid (not the parents), and we have no easy way to get this information inside a form event.

In cases where we need our forms to be used at different places in our application, it could be useful to use those events instead of creating a separate service dedicated to handle them.

Any opinion on this?

Example

  • This could significantly reduce the complexity of this PR where I hash a password only when the form is valid.

But this will also open lot of new possibilities.

  • As a very simple example, we could attach a listener on the form.valid event to persist the entity returned by the form.
    And re attach this event to all the forms we want.
    No need to do this in the controller anymore.
    (Not sure this is the best example I can find)

  • The form.invalid event could be used to attach an event dedicated to save metrics about failed form submissions to improve UX of our applications. (analysis of where most customers fail to submit forms)

@carsonbot carsonbot added the Form label Jul 24, 2022
@Seb33300 Seb33300 changed the title [Form] Adding valid & invalid events [Form] Adding valid & invalid form events Jul 24, 2022
@MichaelBrauner
Copy link

MichaelBrauner commented Aug 2, 2022

I wrote a Bundle for that.
But atm it only supports valid. But you are right - it could also be a good idea to add an invalid Event.

When it would be implemented into the symfony form component.
That would of course be really nice.

@nicolas-grekas
Copy link
Member

We just closed #37641 if you want to have a look.

@Seb33300
Copy link
Contributor Author

Seb33300 commented Aug 2, 2022

Interesting. I did not find it when searching.
Latest PR is #38479

cc @alcaeus

I will try to take a look at it if I can find the time

@alcaeus
Copy link
Contributor

alcaeus commented Aug 2, 2022

@Seb33300 feel free to reuse my code and continue where I left off 👍

@Seb33300 Seb33300 linked a pull request Aug 6, 2022 that will close this issue
@Seb33300
Copy link
Contributor Author

Seb33300 commented Aug 6, 2022

I created a PR: #47210

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants