-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[Validator] added improve support for collection validation #31196
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
[Validator] added improve support for collection validation #31196
Conversation
Can you please squash + review the CS? |
fcfb4af
to
36f0176
Compare
done @nicolas-grekas |
654a846
to
1f7be1f
Compare
Can you add an entry to the Validator component |
changelog added, PR rebased with master. |
1f7be1f
to
fb26d1f
Compare
Validators and test rewrited. |
d16e041
to
9206395
Compare
src/Symfony/Component/Validator/Constraints/AbstractComposite.php
Outdated
Show resolved
Hide resolved
src/Symfony/Component/Validator/Tests/Constraints/AbstractCompositeTest.php
Outdated
Show resolved
Hide resolved
9206395
to
20f9edd
Compare
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.
Some typos.
src/Symfony/Component/Validator/Tests/Validator/RecursiveValidatorTest.php
Outdated
Show resolved
Hide resolved
src/Symfony/Component/Validator/Constraints/AbstractComposite.php
Outdated
Show resolved
Hide resolved
src/Symfony/Component/Validator/Constraints/AbstractComposite.php
Outdated
Show resolved
Hide resolved
20f9edd
to
59a8f1d
Compare
Thanks @elementaire for the review. |
src/Symfony/Component/Validator/Constraints/AbstractComposite.php
Outdated
Show resolved
Hide resolved
src/Symfony/Component/Validator/Constraints/AbstractComposite.php
Outdated
Show resolved
Hide resolved
src/Symfony/Component/Validator/Tests/Constraints/AbstractCompositeTest.php
Outdated
Show resolved
Hide resolved
src/Symfony/Component/Validator/Tests/Constraints/AbstractCompositeTest.php
Outdated
Show resolved
Hide resolved
src/Symfony/Component/Validator/Tests/Constraints/AbstractCompositeTest.php
Outdated
Show resolved
Hide resolved
src/Symfony/Component/Validator/Tests/Constraints/AbstractCompositeTest.php
Outdated
Show resolved
Hide resolved
59a8f1d
to
4d05d1d
Compare
d6efa82
to
6501f58
Compare
Thanks for the review @xabbuh, all comments have been resolved ;). Status: Needs review |
Travis failure is not related |
PR Rebased |
6501f58
to
04ab76c
Compare
04ab76c
to
a9aa221
Compare
@@ -18,6 +18,8 @@ | |||
* @Target({"PROPERTY", "METHOD", "ANNOTATION"}) | |||
* | |||
* @author Yevgeniy Zholkevskiy <zhenya.zholkevskiy@gmail.com> | |||
* @author Marc Morera Merino <yuhu@mmoreram.com> | |||
* @author Marc Morales Valldepérez <marcmorales83@gmail.com> |
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.
Why these changes? And if, shouldn't they be done on the 4.3
branch?
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.
was from the original PR
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 think it needs to be revert because it was the contraints has been modfied in the original PR WDYT ? @xabbuh
/** | ||
* {@inheritdoc} | ||
*/ | ||
protected function getCompositeOption() |
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 do not really see why we need this method.
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 say if it's a contraints or a field, if we remove it, we need to remove it from the abtracts composite too and check if it's a contraints or a field
src/Symfony/Component/Validator/Constraints/AbstractComposite.php
Outdated
Show resolved
Hide resolved
/** | ||
* @author Hamza Amrouche <hamza.simperfit@gmail.com> | ||
*/ | ||
class ExactlyValidator extends ConstraintValidator |
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 am actually not convinced that we need this validator. What would be a use case for it?
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.
because of #31196 (comment)
* @author Marc Morera Merino <yuhu@mmoreram.com> | ||
* @author Marc Morales Valldepérez <marcmorales83@gmail.com> | ||
*/ | ||
class SomeValidator extends ConstraintValidator |
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.
What would be an actual use case for this constraint?
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 guess if some of the required contraints passesa and some other not it should be working properly.
a9aa221
to
98cd0d4
Compare
98cd0d4
to
10948fc
Compare
PR Rebased and review answered. Status: Needs Review |
cc @xabbuh another round ? |
@Simperfit I would really be interested in seeing a real concrete use case where one would like to ensure that a particular number of constraints passes (or fails). I cannot come up with an example where this seems to make sense. |
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.
Almost all validators need to be re-implemented in that PR.
* added `Each` constraint | ||
* added `EachValidator` | ||
* added `None` constraint | ||
* added `NoneValidator` |
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 suggest merging this line with the previous one. Adding the new constraint and its validator are a single feature addition (same for other constraints).
Older versions don't mention the validator at all btw, only the new constraint (as that's the relevant user-facing thing)
use Symfony\Component\Validator\Exception\ConstraintDefinitionException; | ||
|
||
/** | ||
* @Annotation |
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.
should be removed. This abstract class is not an annotation by itself.
@@ -16,23 +16,12 @@ | |||
* @Target({"PROPERTY", "METHOD", "ANNOTATION"}) | |||
* | |||
* @author Bernhard Schussek <bschussek@gmail.com> | |||
* | |||
* @api |
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.
should be removed. We dropped such tag years ago in favor of @internal
and @experimental
* | ||
* @api | ||
* | ||
* @deprecated Deprecated in 4.3, to be removed in 5.0. Use |
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.
4.4
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.
and this also needs to trigger a deprecation warning.
* | ||
* @api | ||
* | ||
* @deprecated Deprecated in 4.3, to be removed in 5.0. Use |
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.
same issues here
throw new UnexpectedValueException($value, 'array or Traversable'); | ||
} | ||
|
||
$validator = $this->context->getValidator()->inContext($this->context); |
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.
same here about not using the current context for violations you don't want to show.
* | ||
* Message for notice Max Violation | ||
*/ | ||
public $maxMessage = '{{ limit }} or less element of this collection should pass validation.|{{ limit }} or less elements of this collection should pass validation.'; |
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.
no message for the exactly case ?
* | ||
* Exactly number of Success expected | ||
*/ | ||
public $exactly; |
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.
Do we need this property ? Isn't it the same than min = max
?
|
||
$totalIterations = \count($value) * \count($constraint->constraints); | ||
|
||
$validator = $this->context->getValidator()->inContext($this->context); |
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.
same issue here about not reusing the context
$validator->atPath('['.$key.']')->validate($element, $constraint->constraints); | ||
} | ||
|
||
$constraintsSuccess = $totalIterations - (int) $this->context->getViolations()->count(); |
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 wrong IMO. You should not count how many constraints were successful, but how many elements were successful. Otherwise, this is a pain to get right.
I've taken #9988, what's missing here @webmozart ?