forked from symfony/symfony
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
create oneof constraint and add deprecation messages in choice, also …
…make choice extend new oneOf constraint to avoid duplicate code
- Loading branch information
1 parent
c0507aa
commit 9071676
Showing
7 changed files
with
193 additions
and
112 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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,41 @@ | ||
<?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\Constraints; | ||
|
||
use Symfony\Component\Validator\Constraint; | ||
|
||
/** | ||
* @Annotation | ||
* | ||
* @api | ||
*/ | ||
class OneOf extends Constraint | ||
{ | ||
public $choices; | ||
public $callback; | ||
public $multiple = false; | ||
public $strict = false; | ||
public $min = null; | ||
public $max = null; | ||
public $message = 'The value you selected is not a valid choice.'; | ||
public $multipleMessage = 'One or more of the given values is invalid.'; | ||
public $minMessage = 'You must select at least {{ limit }} choices.'; | ||
public $maxMessage = 'You must select at most {{ limit }} choices.'; | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
public function getDefaultOption() | ||
{ | ||
return 'choices'; | ||
} | ||
} |
83 changes: 83 additions & 0 deletions
83
src/Symfony/Component/Validator/Constraints/OneOfValidator.php
This file contains 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,83 @@ | ||
<?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\Constraints; | ||
|
||
use Symfony\Component\Validator\Constraint; | ||
use Symfony\Component\Validator\ConstraintValidator; | ||
use Symfony\Component\Validator\Exception\ConstraintDefinitionException; | ||
use Symfony\Component\Validator\Exception\UnexpectedTypeException; | ||
|
||
/** | ||
* ChoiceValidator validates that the value is one of the expected values. | ||
* | ||
* @author Fabien Potencier <fabien@symfony.com> | ||
* @author Florian Eckerstorfer <florian@eckerstorfer.org> | ||
* @author Bernhard Schussek <bschussek@gmail.com> | ||
* | ||
* @api | ||
*/ | ||
class OneOfValidator extends ConstraintValidator | ||
{ | ||
/** | ||
* {@inheritDoc} | ||
*/ | ||
public function validate($value, Constraint $constraint) | ||
{ | ||
if (!$constraint->choices && !$constraint->callback) { | ||
throw new ConstraintDefinitionException('Either "choices" or "callback" must be specified on constraint Choice'); | ||
} | ||
|
||
if (null === $value) { | ||
return; | ||
} | ||
|
||
if ($constraint->multiple && !is_array($value)) { | ||
throw new UnexpectedTypeException($value, 'array'); | ||
} | ||
|
||
if ($constraint->callback) { | ||
if (is_callable(array($this->context->getCurrentClass(), $constraint->callback))) { | ||
$choices = call_user_func(array($this->context->getCurrentClass(), $constraint->callback)); | ||
} elseif (is_callable($constraint->callback)) { | ||
$choices = call_user_func($constraint->callback); | ||
} else { | ||
throw new ConstraintDefinitionException('The OneOf constraint expects a valid callback'); | ||
} | ||
} else { | ||
$choices = $constraint->choices; | ||
} | ||
|
||
if ($constraint->multiple) { | ||
foreach ($value as $_value) { | ||
if (!in_array($_value, $choices, $constraint->strict)) { | ||
$this->context->addViolation($constraint->multipleMessage, array('{{ value }}' => $_value)); | ||
} | ||
} | ||
|
||
$count = count($value); | ||
|
||
if ($constraint->min !== null && $count < $constraint->min) { | ||
$this->context->addViolation($constraint->minMessage, array('{{ limit }}' => $constraint->min), null, (int) $constraint->min); | ||
|
||
return; | ||
} | ||
|
||
if ($constraint->max !== null && $count > $constraint->max) { | ||
$this->context->addViolation($constraint->maxMessage, array('{{ limit }}' => $constraint->max), null, (int) $constraint->max); | ||
|
||
return; | ||
} | ||
} elseif (!in_array($value, $choices, $constraint->strict)) { | ||
$this->context->addViolation($constraint->message, array('{{ value }}' => $value)); | ||
} | ||
} | ||
} |
Oops, something went wrong.