-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[Validator] added improve support for collection validation #9988
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
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
121 changes: 121 additions & 0 deletions
121
src/Symfony/Component/Validator/Constraints/AbstractComposite.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\Validator\Constraints; | ||
|
||
use Symfony\Component\Validator\Constraint; | ||
use Symfony\Component\Validator\Exception\ConstraintDefinitionException; | ||
|
||
/** | ||
* @Annotation | ||
* | ||
* @author Marc Morales Valldepérez <[email protected]> | ||
* @author Marc Morera Merino <[email protected]> | ||
*/ | ||
abstract class AbstractComposite extends Constraint | ||
{ | ||
|
||
/** | ||
* @var array | ||
* | ||
* Set of constraints | ||
*/ | ||
public $constraints = array(); | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
public function __construct($options = null) | ||
{ | ||
parent::__construct($options); | ||
|
||
if (!is_array($this->constraints)) { | ||
$this->constraints = array($this->constraints); | ||
} | ||
|
||
// We consider explicid groups are defined if are not default one | ||
$areExplicitGroupsDefined = ( $this->groups != array(self::DEFAULT_GROUP)); | ||
|
||
// Each constraint contained | ||
foreach ($this->constraints as $constraint) { | ||
if (!$constraint instanceof Constraint) { | ||
throw new ConstraintDefinitionException(sprintf('The value %s is not an instance of Constraint in constraint %s', $constraint, __CLASS__)); | ||
} | ||
|
||
if ($constraint instanceof Valid) { | ||
throw new ConstraintDefinitionException(sprintf('The constraint Valid cannot be nested inside constraint %s. You can only declare the Valid constraint directly on a field or method.', __CLASS__)); | ||
} | ||
|
||
// If explicid groups are defined | ||
if ($areExplicitGroupsDefined) { | ||
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 can use |
||
/** | ||
* If constraint has explicid groups defined | ||
* | ||
* In that case, the groups of the nested constraint need to be | ||
* a subset of the groups of the outer constraint. | ||
*/ | ||
if ($constraint->groups !== array(self::DEFAULT_GROUP)) { | ||
|
||
// If are not a subset | ||
if ($constraint->groups != array_intersect($constraint->groups, $this->groups)) { | ||
throw new ConstraintDefinitionException(sprintf('The groups defined in Constraint %s must be a subset of the groups defined in the Constraint %s', $constraint, __CLASS__)); | ||
} | ||
|
||
// Otherwise, we add all defined groups here | ||
} else { | ||
foreach ($this->groups as $group) { | ||
$constraint->addImplicitGroupName($group); | ||
} | ||
} | ||
|
||
/** | ||
* Otherwise, we merge current groups with constraint | ||
*/ | ||
} else { | ||
$this->groups = array_unique(array_merge($this->groups, $constraint->groups)); | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* Adds the given group if this constraint is in the Default group. | ||
* | ||
* Also propagate same method to nested Constraints. | ||
* | ||
* @param string $group | ||
* | ||
* @api | ||
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. Please remove the |
||
*/ | ||
public function addImplicitGroupName($group) | ||
{ | ||
parent::addImplicitGroupName($group); | ||
|
||
foreach ($this->constraints as $constraint) { | ||
$constraint->addImplicitGroupName($group); | ||
} | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
public function getDefaultOption() | ||
{ | ||
return 'constraints'; | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
public function getRequiredOptions() | ||
{ | ||
return array('constraints'); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,6 +20,9 @@ | |
* @author Bernhard Schussek <[email protected]> | ||
* | ||
* @api | ||
* | ||
* @deprecated Deprecated in 2.5, to be removed in 3.0. Use | ||
* {@link \Symfony\Component\Validator\Constraints\Each} instead. | ||
*/ | ||
class All extends Constraint | ||
{ | ||
|
@@ -56,4 +59,5 @@ public function getRequiredOptions() | |
{ | ||
return array('constraints'); | ||
} | ||
|
||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,6 +19,9 @@ | |
* @author Bernhard Schussek <[email protected]> | ||
* | ||
* @api | ||
* | ||
* @deprecated Deprecated in 2.5, to be removed in 3.0. Use | ||
* {@link \Symfony\Component\Validator\Constraints\EachValidator} instead. | ||
*/ | ||
class AllValidator extends ConstraintValidator | ||
{ | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\Validator\Constraints; | ||
|
||
/** | ||
* @Annotation | ||
* | ||
* @author Marc Morera Merino <[email protected]> | ||
* @author Marc Morales Valldepérez <[email protected]> * | ||
*/ | ||
class Each extends AbstractComposite | ||
{ | ||
} |
46 changes: 46 additions & 0 deletions
46
src/Symfony/Component/Validator/Constraints/EachValidator.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\Validator\Constraints; | ||
|
||
use Symfony\Component\Validator\Constraint; | ||
use Symfony\Component\Validator\ConstraintValidator; | ||
use Symfony\Component\Validator\Exception\UnexpectedTypeException; | ||
|
||
/** | ||
* @author Marc Morera Merino <[email protected]> | ||
* @author Marc Morales Valldepérez <[email protected]> | ||
*/ | ||
class EachValidator extends ConstraintValidator | ||
{ | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
public function validate($value, Constraint $constraint) | ||
{ | ||
if (null === $value) { | ||
return; | ||
} | ||
|
||
if (!is_array($value) && !$value instanceof \Traversable) { | ||
throw new UnexpectedTypeException($value, 'array or Traversable'); | ||
} | ||
|
||
$group = $this->context->getGroup(); | ||
|
||
foreach ($value as $key => $element) { | ||
foreach ($constraint->constraints as $constr) { | ||
$this->context->validateValue($element, $constr, '[' . $key . ']', $group); | ||
} | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\Validator\Constraints; | ||
|
||
/** | ||
* @Annotation | ||
* | ||
* @author Marc Morera Merino <[email protected]> | ||
* @author Marc Morales Valldepérez <[email protected]> | ||
*/ | ||
class None extends AbstractComposite | ||
{ | ||
|
||
/** | ||
* @var string | ||
* | ||
* Message for notice Violation | ||
*/ | ||
public $message = 'None of this collection should pass validation.'; | ||
} |
57 changes: 57 additions & 0 deletions
57
src/Symfony/Component/Validator/Constraints/NoneValidator.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\Validator\Constraints; | ||
|
||
use Symfony\Component\Validator\Constraint; | ||
use Symfony\Component\Validator\ConstraintValidator; | ||
use Symfony\Component\Validator\Exception\UnexpectedTypeException; | ||
|
||
/** | ||
* @author Marc Morera Merino <[email protected]> | ||
* @author Marc Morales Valldepérez <[email protected]> | ||
*/ | ||
class NoneValidator extends ConstraintValidator | ||
{ | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
public function validate($value, Constraint $constraint) | ||
{ | ||
if (null === $value) { | ||
return; | ||
} | ||
|
||
if (!is_array($value) && !$value instanceof \Traversable) { | ||
throw new UnexpectedTypeException($value, 'array or Traversable'); | ||
} | ||
|
||
$group = $this->context->getGroup(); | ||
|
||
$totalIterations = count($value) * count($constraint->constraints); | ||
|
||
foreach ($value as $key => $element) { | ||
foreach ($constraint->constraints as $constr) { | ||
$this->context->validateValue($element, $constr, '[' . $key . ']', $group); | ||
} | ||
} | ||
|
||
$constraintsSuccess = $totalIterations - (int) $this->context->getViolations()->count(); | ||
|
||
//We clear all violations as just current Validator should add real Violations | ||
$this->context->clearViolations(); | ||
|
||
if ($constraintsSuccess > 0) { | ||
$this->context->addViolation($constraint->message); | ||
} | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
you should remove this empty line