-
-
Notifications
You must be signed in to change notification settings - Fork 5.2k
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
Added callable validation_groups example #6579
Conversation
public function configureOptions(OptionsResolver $resolver) | ||
{ | ||
$resolver->setDefaults(array( | ||
'validation_groups' => $this->groupResolver, |
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.
Since at this point your are invoking your group resolver through the magic __invoke()
method, how is this different that for instance invoking an specific method of your ValidationGroupResolver
class? something like: $this->groupResolver->someMethod()
.
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.
The method is not invoked at this point. It is invoked by the form validator when transforming this option to a value. So the difference is the moment of calling.
I think having such a big new thing in the book article doesn't make much sense. It's quite advanced. Can you maybe move this to a new cookbook article (e.g. "How to dynamically configure validation groups") and then just link to this cookbook article from the book article with a |
If you need some advanced logic to determine the validation groups that cannot | ||
be simply determined by a simple callback (for example that requires a few | ||
services) you can use a service. Implement a service that implements __invoke | ||
which accepts a FormInterface as a parameter. |
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 indicate that the next block is a PHP code block, you should end this sentence with ::
or add a new line with .. code-block:: php
Ok, I think I've made all requested modifications. I'm not as familiar with RST so its quite possible that its not yet perfect so just let me know. |
This will result in the form validator invoking your group resolver to | ||
set the validation groups returned when validating. | ||
|
||
|
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.
these 2 empty lines can be removed
@gnat42 I've left quite a lot comments again, but they aren't very big ones (just minor formatting and wording changes). After these changes, I think this PR is ready (and we can apply these changes during merging as well if you don't have much time). |
@@ -0,0 +1,55 @@ | |||
How to dynamically configure validation groups | |||
============================================= |
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 one =
too short
Hey @wouterj no worries, this is the way we maintain high quality code and documentation. See the latest version with your changes applied. Let me know if there are any others. |
$resolver->setDefaults(array( | ||
'validation_groups' => $this->groupResolver, | ||
)); | ||
} |
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.
missing the closing }
of the class
Left a few again and you have to add this article to |
You also have to add references to the new file to |
Recently I had a need to dynamically set the validation_groups of a small set of forms based on the user that was logged in. In digging about the FormValidator::resolveValidationGroups and saw I could use a class that implements the __invoke method. Figured I might as well write something about it and see if you'd want to add it to the docs.
Sorry, somehow got a typo in that last one. All tests pass. Let me know if there are any other things needing changing. |
👍 |
This PR was submitted for the 3.0 branch but it was merged into the 2.7 branch instead (closes #6579). Discussion ---------- Added callable validation_groups example Recently I had a need to dynamically set the validation_groups of a small set of forms based on the user that was logged in. In digging about the FormValidator::resolveValidationGroups and saw I could use a class that implements the __invoke method. Figured I might as well write something about it and see if you'd want to add it to the docs. Commits ------- 6a96a0c Added callable validation_groups example
* 2.7: [#6579] some tweaks Added callable validation_groups example Fix console.exception and console.terminate order Update options_resolver.rst Fixed a typo in cookbook/security/entity_provider Added a note about coding standards and method arguments reflect the EOM of Symfony 2.3
* 2.8: [#6579] some tweaks Added callable validation_groups example Fix console.exception and console.terminate order Update options_resolver.rst Fixed a typo in cookbook/security/entity_provider Added a note about coding standards and method arguments reflect the EOM of Symfony 2.3
* 3.0: [#6579] some tweaks Added callable validation_groups example Fix console.exception and console.terminate order Update options_resolver.rst Fixed a typo in cookbook/security/entity_provider Added a note about coding standards and method arguments reflect the EOM of Symfony 2.3
Recently I had a need to dynamically set the validation_groups of a small set of forms based on the user that was logged in. In digging about the FormValidator::resolveValidationGroups and saw I could use a class that implements the __invoke method. Figured I might as well write something about it and see if you'd want to add it to the docs.