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

Added callable validation_groups example #6579

Closed
wants to merge 1 commit into from
Closed

Conversation

gnat42
Copy link
Contributor

@gnat42 gnat42 commented May 19, 2016

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.

public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array(
'validation_groups' => $this->groupResolver,
Copy link
Contributor

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().

Copy link
Member

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.

@wouterj
Copy link
Member

wouterj commented May 21, 2016

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 .. seealso:: box?

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.
Copy link
Member

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

@gnat42
Copy link
Contributor Author

gnat42 commented May 21, 2016

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.


Copy link
Member

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

@wouterj
Copy link
Member

wouterj commented May 21, 2016

@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
=============================================
Copy link
Member

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

@gnat42
Copy link
Contributor Author

gnat42 commented May 21, 2016

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,
));
}
Copy link
Member

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

@wouterj
Copy link
Member

wouterj commented May 21, 2016

Left a few again and you have to add this article to /cookbook/map.rst.inc and /cookbook/validation/index.rst

@xabbuh
Copy link
Member

xabbuh commented May 21, 2016

You also have to add references to the new file to /cookbook/map.rst.inc and /cookbook/validation/index.rst.

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.
@gnat42
Copy link
Contributor Author

gnat42 commented May 23, 2016

Sorry, somehow got a typo in that last one. All tests pass. Let me know if there are any other things needing changing.

@wouterj
Copy link
Member

wouterj commented May 26, 2016

👍

xabbuh added a commit that referenced this pull request Jun 6, 2016
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
xabbuh added a commit that referenced this pull request Jun 6, 2016
@xabbuh
Copy link
Member

xabbuh commented Jun 6, 2016

Thank you for this nice contribution @gnat42! I have merged it into the 2.7 branch and made some small tweaks to it in f1329d0.

@xabbuh xabbuh closed this Jun 6, 2016
xabbuh added a commit that referenced this pull request Jun 6, 2016
* 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
xabbuh added a commit that referenced this pull request Jun 6, 2016
* 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
xabbuh added a commit that referenced this pull request Jun 6, 2016
* 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
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.

None yet

4 participants