-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[Validator] New NodeTraverser implementation #10287
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
Changes from 1 commit
25cdc68
a6ed4ca
a40189c
7e3a41d
b1a9477
1156bde
321d5bb
680f1ee
8ae68c9
c1b1e03
5fbf848
8318286
f6b7288
4ea3ff6
adc1437
499b2bb
405a03b
9b07b0c
297ba4f
09f744b
ee1adad
718601c
feb3d6f
1e81f3b
9c9e715
2c65a28
a3555fb
bc29591
26eafa4
df41974
e440690
e057b19
230f2a7
cf1281f
94583a9
117b1b9
51197f6
08172bf
aeb6822
4161371
76d8c9a
778ec24
6fc6ecd
2936d10
e8fa15b
8558377
dbce5a2
822fe47
186c115
299c2dc
be7f055
9986f03
524a953
9ca61df
01ceeda
79387a7
987313d
93fdff7
886e05e
f61d31e
23534ca
38e26fb
274d4e6
eeed509
5c479d8
eed29d8
50bb84d
be508e0
1622eb3
94ef21e
73c9cc5
2f23d97
029a716
3183aed
90c27bb
166d71a
7bc952d
1b111d0
0946dbe
9b204c9
c5629bb
3dc2b4d
0bfde4a
b1badea
68d8018
ca6a722
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,10 +11,44 @@ | |
|
||
namespace Symfony\Component\Validator\Constraints; | ||
|
||
use Symfony\Component\Validator\Exception\OutOfBoundsException; | ||
use Traversable; | ||
|
||
/** | ||
* Annotation for group sequences | ||
* A sequence of validation groups. | ||
* | ||
* When validating a group sequence, each group will only be validated if all | ||
* of the previous groups in the sequence succeeded. For example: | ||
* | ||
* $validator->validateObject($address, new GroupSequence('Basic', 'Strict')); | ||
* | ||
* In the first step, all constraints that belong to the group "Basic" will be | ||
* validated. If none of the constraints fail, the validator will then validate | ||
* the constraints in group "Strict". This is useful, for example, if "Strict" | ||
* contains expensive checks that require a lot of CPU or slow, external | ||
* services. You usually don't want to run expensive checks if any of the cheap | ||
* checks fails. | ||
* | ||
* When adding metadata to a class, you can override the "Default" group of | ||
* that class with a group sequence: | ||
* | ||
* /** | ||
* * @GroupSequence({"Address", "Strict"}) | ||
* *\/ | ||
* class Address | ||
* { | ||
* // ... | ||
* } | ||
* | ||
* Whenever you validate that object in the "Default" group, the group sequence | ||
* will be validated: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This point was, and stays a bit hard to understand.
I want to highlight a naming issue here. In the past, I was a bit lost between There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I know, this is one of the confusing parts of JSR-303. Suppose you have constraints in the "Default" group: class Address
{
/**
* @Length(min=3, groups="Default")
*/
private $street;
} Then these two calls will be 100% equivalent: $violations = $validator->validate($address, "Default");
$violations = $validator->validate($address, "Address"); We say that "Address" is the implicit group name of those constraints, i.e. a group name that is equivalent to "Default". Let's override the default group with a sequence: /**
* @GroupSequence({"Address", "Strict"})
*/
class Address
{
// ... rest stays the same ...
} Now the calls are not equivalent anymore: // validates the group sequence
$violations = $validator->validate($address, "Default");
// validates the constraints in "Default"
$violations = $validator->validate($address, "Address"); That is also why you need to refer to the "Default" group as "" in the sequence. The following would create a recursion and is invalid (remember that "Default" === the group sequence now): /**
* @GroupSequence({"Default", "Strict"})
*/
class Address
{
// ... rest stays the same ...
} There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Btw, if you think you can improve the documentation about this functionality, please with sugar on top do so :) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ok understood. Thanks. I think we can copy/paste your explanation for the doc. ping @wouterj There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
* | ||
* $validator->validateObject($address); | ||
* | ||
* If you want to execute the constraints of the "Default" group for a class | ||
* with an overridden default group, pass the class name as group name instead: | ||
* | ||
* $validator->validateObject($address, "Address") | ||
* | ||
* @Annotation | ||
* | ||
|
@@ -25,13 +59,14 @@ | |
class GroupSequence implements \ArrayAccess, \IteratorAggregate, \Countable | ||
{ | ||
/** | ||
* The members of the sequence | ||
* @var array | ||
* The groups in the sequence | ||
* | ||
* @var string[]|GroupSequence[] | ||
*/ | ||
public $groups; | ||
|
||
/** | ||
* The group under which cascaded objects are validated when validating | ||
* The group in which cascaded objects are validated when validating | ||
* this sequence. | ||
* | ||
* By default, cascaded objects are validated in each of the groups of | ||
|
@@ -46,27 +81,80 @@ class GroupSequence implements \ArrayAccess, \IteratorAggregate, \Countable | |
*/ | ||
public $cascadedGroup; | ||
|
||
/** | ||
* Creates a new group sequence. | ||
* | ||
* @param string[] $groups The groups in the sequence | ||
*/ | ||
public function __construct(array $groups) | ||
{ | ||
// Support for Doctrine annotations | ||
$this->groups = isset($groups['value']) ? $groups['value'] : $groups; | ||
} | ||
|
||
/** | ||
* Returns an iterator for this group. | ||
* | ||
* @return Traversable The iterator | ||
* | ||
* @see \IteratorAggregate::getIterator() | ||
* | ||
* @deprecated Implemented for backwards compatibility. To be removed in | ||
* Symfony 3.0. | ||
*/ | ||
public function getIterator() | ||
{ | ||
return new \ArrayIterator($this->groups); | ||
} | ||
|
||
/** | ||
* Returns whether the given offset exists in the sequence. | ||
* | ||
* @param integer $offset The offset | ||
* | ||
* @return Boolean Whether the offset exists | ||
* | ||
* @deprecated Implemented for backwards compatibility. To be removed in | ||
* Symfony 3.0. | ||
*/ | ||
public function offsetExists($offset) | ||
{ | ||
return isset($this->groups[$offset]); | ||
} | ||
|
||
/** | ||
* Returns the group at the given offset. | ||
* | ||
* @param integer $offset The offset | ||
* | ||
* @return string The group a the given offset | ||
* | ||
* @throws OutOfBoundsException If the object does not exist | ||
* | ||
* @deprecated Implemented for backwards compatibility. To be removed in | ||
* Symfony 3.0. | ||
*/ | ||
public function offsetGet($offset) | ||
{ | ||
if (!isset($this->groups[$offset])) { | ||
throw new OutOfBoundsException(sprintf( | ||
'The offset "%s" does not exist.', | ||
$offset | ||
)); | ||
} | ||
|
||
return $this->groups[$offset]; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. you need to take care of error handling here to avoid a notice on invalid access There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
:) |
||
} | ||
|
||
/** | ||
* Sets the group at the given offset. | ||
* | ||
* @param integer $offset The offset | ||
* @param string $value The group name | ||
* | ||
* @deprecated Implemented for backwards compatibility. To be removed in | ||
* Symfony 3.0. | ||
*/ | ||
public function offsetSet($offset, $value) | ||
{ | ||
if (null !== $offset) { | ||
|
@@ -78,11 +166,27 @@ public function offsetSet($offset, $value) | |
$this->groups[] = $value; | ||
} | ||
|
||
/** | ||
* Removes the group at the given offset. | ||
* | ||
* @param integer $offset The offset | ||
* | ||
* @deprecated Implemented for backwards compatibility. To be removed in | ||
* Symfony 3.0. | ||
*/ | ||
public function offsetUnset($offset) | ||
{ | ||
unset($this->groups[$offset]); | ||
} | ||
|
||
/** | ||
* Returns the number of groups in the sequence. | ||
* | ||
* @return integer The number of groups | ||
* | ||
* @deprecated Implemented for backwards compatibility. To be removed in | ||
* Symfony 3.0. | ||
*/ | ||
public function count() | ||
{ | ||
return count($this->groups); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\Validator\Exception; | ||
|
||
/** | ||
* Base OutOfBoundsException for the Validator component. | ||
* | ||
* @author Bernhard Schussek <bschussek@gmail.com> | ||
*/ | ||
class OutOfBoundsException extends \OutOfBoundsException implements ExceptionInterface | ||
{ | ||
} |
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.
fail
as any is plural right?