10000 Clarified Default and ClassName groups by wouterj · Pull Request #3593 · symfony/symfony-docs · GitHub
[go: up one dir, main page]

Skip to content

Clarified Default and ClassName groups #3593

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
merged 2 commits into from
Mar 8, 2014
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Clarified Default vs ClassName groups
  • Loading branch information
wouterj committed Feb 20, 2014
commit 2330cd8c2cc5f57be430fcde275220b75a09860f
30 changes: 20 additions & 10 deletions book/validation.rst
Original file line number Diff line number Diff line change
Expand Up @@ -807,10 +807,11 @@ user registers and when a user updates their contact information later:
}
}

With this configuration, there are two validation groups:
With this configuration, there are three validation groups:

* ``User`` - contains the constraints that belong to no other group,
and is considered the ``Default`` group. (This group is useful for
* ``User`` - contains the constraints that belong to no other group;

* ``Default`` - equivalent to the ``User`` group (This group is useful for
Copy link
Contributor

Choose a reason for hiding this comment

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

IMO this is the wrong order. All constraints belong to the "Default" group first and foremost. "User" is just an alias for the constraints in the "Default" group of the User class. That means, for example:

class User extends Person
{
    /**
     * @Assert\Valid
     * @Assert\Type("Acme\Address")
     */
    private $address;
}
  • When I validate a User object in the "Default" group, all default constraints of both the User and the Address will be validated.
  • When I validate it in the "User" group, only the default constraints of the User class (and superclasses) will be executed
  • When I validate it in the "Address" group, only the default constraints of the Address class will be executed.
  • When I validated it in the "Person" group, only the default constraints of the Person class (and superclasses) will be executed.

Copy link
Contributor

Choose a reason for hiding this comment

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

Btw, you should use CamelCaps notation for group names.

Copy link
Member Author

Choose a reason for hiding this comment

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

About your first comment: Thanks a lot for creating such a nice real world examples! :) It helps me a lot to understand the topic. I think I've covered it now in the updated list.

And your second comment: I can't see a place where I don't use CamelCaps?

:ref:`book-validation-group-sequence`);

* ``registration`` - contains the constraints on the ``email`` and ``password``
Expand All @@ -837,13 +838,8 @@ Group Sequence
--------------

In some cases, you want to validate your groups by steps. To do this, you can
use the ``GroupSequence`` feature. In the case, an object defines a group sequence,
and then the groups in the group sequence are validated in order.

.. tip::

Group sequences cannot contain the group ``Default``, as this would create
a loop. Instead, use the group ``{ClassName}`` (e.g. ``User``).
use the ``GroupSequence`` feature. In this case, an object defines a group sequence
, which determines the order groups should be validated.
Copy link
Contributor

Choose a reason for hiding this comment

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

This is a little misleading. Right now, it's kind of true, but as of 2.5 this will be wrong.

The important fact is not whether or not I use group sequences. In 2.5, for example, the following will be possible:

$validator->validate($object, new GroupSequence('Default', 'Strict'));

You see how this group sequence contains the "Default" group? And it's perfectly fine.

The reason why the group sequence can't contain the "Default" group here is that, by putting it on the class, we're replacing the "Default" group with the group sequence.

Copy link
Member Author

Choose a reason for hiding this comment

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

I think this comment should be some lines down.

However, you mean that the Default group will only be the group sequence if it's defined in the metadata of a class?

Copy link
Member

Choose a reason for hiding this comment

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

If we need to clarify this more or change for 2.5, I'll leave it for then so we can merge this :)


For example, suppose you have a ``User`` class and want to validate that the
username and the password are different only if all other validation passes
Expand Down Expand Up @@ -968,6 +964,20 @@ In this example, it will first validate all constraints in the group ``User``
(which is the same as the ``Default`` group). Only if all constraints in
that group are valid, the second group, ``Strict``, will be validated.

.. caution::

As you have already seen in the previous section, the ``Default`` group
and the group containing the class name (e.g. ``User``) were identical.
However, when using Group Sequences, they are no longer identical. The
``Default`` group will now reference the group sequence, instead of all
constraints that do not belong to any group.

This means that you have to use the ``{ClassName}`` (e.g. ``User``) group
when specifing a group sequence. When using ``Default``, you get an
infinite recursion (as the ``Default`` groups references the group
sequence, which will contain the ``Default`` group which references the
same group sequence, ...).

Group Sequence Providers
~~~~~~~~~~~~~~~~~~~~~~~~

Expand Down
0