Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork9.6k
[Form] Introduce validation events#47210
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
base:7.4
Are you sure you want to change the base?
Conversation
64603b1
to134ff4b
CompareIn case you want to review, I saw you participated to the previous PR |
src/Symfony/Component/Form/Extension/Validator/EventListener/ValidationListener.phpShow resolvedHide resolved
Uh oh!
There was an error while loading.Please reload this page.
src/Symfony/Component/Form/Extension/Validator/ValidatorFormEvents.php OutdatedShow resolvedHide resolved
Uh oh!
There was an error while loading.Please reload this page.
src/Symfony/Component/Form/Extension/Validator/EventListener/ValidationListener.phpShow resolvedHide resolved
Uh oh!
There was an error while loading.Please reload this page.
d-ph commentedAug 7, 2022 • edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
Hi. Just wanted to say that I'm happy to see this feature gaining traction, and looking forward to seeing it in the stock Symfony code. Unfortunately, it's unlikely I'd review the PR in depth, however I did skim through the change, and it looks good to me. May I ask a quick question though, just to double-check: Given the following case:
Then:
Thanks. Btw Seb33300 search for "->idValid(" on this page and consider correcting the typos, because it takes a few seconds to conclude that it's a typo. Cheers. |
Seb33300 commentedAug 7, 2022 • edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
@d-ph typo fixed. Thanks. POST_VALIDATE event happens AFTER the form validation has been processed ON ALL form items.
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
Thanks for this proposal. I've now a better idea about the problem to solve after reading your code and description. I left you one comment to improve the implementation based on your approach and the other two are mainly consistency-related concerns.
I'm wondering if dispatching these new events inside theFormValidator
makes more sense, is more accurate, and simplifies the implementation too (as the collection of the form tree won't be needed).
Although I've to admit that I'm not entirely sure how useful this feature is or if we are opening the door to the writing of something completely detached from the Form context.
src/Symfony/Component/Form/Extension/Validator/EventListener/ValidationListener.php OutdatedShow resolvedHide resolved
Uh oh!
There was an error while loading.Please reload this page.
if ($form->isRoot()) { | ||
$this->dispatchEvents(ValidatorFormEvents::PRE_VALIDATE); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
I like the goal of this new event but the dispatching moment is a bit confusing to me. If I create a listener in a nested form type, I expect the event to be received just before the current form is validated inFormValidator
and not before the whole validation process of all forms in the tree is started.
The meaning is different in this case from other events.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
To be consistant with POST_VALIDATE event, I decided to dispatch it before the validation of the entire form start.
(see my other comment to know why POST_VALIDATE is dispatched after all form items are validated)
@@ -50,6 +64,24 @@ public function validateForm(FormEvent $event) | |||
$this->violationMapper->mapViolation($violation, $form, $allowNonSynchronized); | |||
} | |||
$this->dispatchEvents(ValidatorFormEvents::POST_VALIDATE); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
I understand that child forms can check easily whether their parent is valid or not, but I have the same confusion about the right moment this event is being dispatched. They are not done just after the current form is validated but when the whole validation process is finished for all forms.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
This is the main purpose of this PR, to be able to know if the entire form is valid or not.
That's why the POST_VALIDATE event must be dispatched here.
If we dispatch the event just after the validation of the current item, we will only be able to know if the current item (and his parents) are valid.
Maybe we can introduce a 3rd event "VALIDATE" that could be dispatched just after the current form item is validated?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
Or rename PRE/POST events by START/FINISH to avoid confusion?
3e869d0
toe23b9d6
CompareI faced the same use case. I did not test this PR, but I think this can be a good feature 👍🏼 |
Uh oh!
There was an error while loading.Please reload this page.
Inspired from#38479 (@alcaeus, I coud not reuse your commit because you deleted your branch)
With some changes:
form.pre_validate
andform.post_validate
events are triggered individually to all Form items, not only the to the root form (same as all others existing form events)Regarding the second point, for example, when working with nested forms, if the
form.post_validate
event is attached to a child form:$event->getForm()->isValid()
will return the validity for the current form item only (the nested one)$event->getForm()->getRoot()->isValid()
Usage example:
Thy are working in the same way as existing form events work.
Why are those events required?
Can't we just add a
POST_SUBMIT
event with a lower priority than the validation event?Yes, this solution can work, but ONLY on the root form.
Because the validation of the form is triggered only by the root form, and events are triggered individually on all form items, starting from ancestors and ending by the root form.
This means, we currently have no way to know if a nested form is valid when attaching a POST_SUBMIT event on it.