diff --git a/UPGRADE-2.1.md b/UPGRADE-2.1.md index ef8756ab0a69..06c8eccfc9a2 100644 --- a/UPGRADE-2.1.md +++ b/UPGRADE-2.1.md @@ -1136,8 +1136,8 @@ private $recursiveCollection; ``` - * The `Size` constraint was deprecated and will be removed in Symfony 2.3. You should - use the constraints `Min` and `Max` instead. + * The `Size`, `Min` and `Max` constraints were deprecated and will be removed in + Symfony 2.3. You should use the new constraint `Range` instead. Before: @@ -1149,10 +1149,21 @@ After: ``` - /** - * @Assert\Min(2) - * @Assert\Max(16) - */ + /** @Assert\Range(min = 2, max = 16) */ + private $numberOfCpus; + ``` + + Before: + + ``` + /** @Assert\Min(2) */ + private $numberOfCpus; + ``` + + After: + + ``` + /** @Assert\Range(min = 2) */ private $numberOfCpus; ``` diff --git a/src/Symfony/Component/Validator/CHANGELOG.md b/src/Symfony/Component/Validator/CHANGELOG.md index f869321899fb..781e88e0f1c2 100644 --- a/src/Symfony/Component/Validator/CHANGELOG.md +++ b/src/Symfony/Component/Validator/CHANGELOG.md @@ -22,4 +22,5 @@ CHANGELOG recursively anymore by default. `Valid` contains a new property `deep` which enables the BC behavior. * deprecated the Size constraint and renamed it to Range + * deprecated the Min and Max constraints * added Count constraint diff --git a/src/Symfony/Component/Validator/Constraints/Max.php b/src/Symfony/Component/Validator/Constraints/Max.php index ffb958e85bac..814fc1f33930 100644 --- a/src/Symfony/Component/Validator/Constraints/Max.php +++ b/src/Symfony/Component/Validator/Constraints/Max.php @@ -17,6 +17,8 @@ * @Annotation * * @api + * + * @deprecated Deprecated since version 2.1, to be removed in 2.3. */ class Max extends Constraint { diff --git a/src/Symfony/Component/Validator/Constraints/MaxValidator.php b/src/Symfony/Component/Validator/Constraints/MaxValidator.php index 18d8e8902ee9..e3e9f8c46286 100644 --- a/src/Symfony/Component/Validator/Constraints/MaxValidator.php +++ b/src/Symfony/Component/Validator/Constraints/MaxValidator.php @@ -18,6 +18,8 @@ * @author Bernhard Schussek * * @api + * + * @deprecated Deprecated since version 2.1, to be removed in 2.3. */ class MaxValidator extends ConstraintValidator { diff --git a/src/Symfony/Component/Validator/Constraints/Min.php b/src/Symfony/Component/Validator/Constraints/Min.php index 78a483329602..6e8b3649b7f5 100644 --- a/src/Symfony/Component/Validator/Constraints/Min.php +++ b/src/Symfony/Component/Validator/Constraints/Min.php @@ -17,6 +17,8 @@ * @Annotation * * @api + * + * @deprecated Deprecated since version 2.1, to be removed in 2.3. */ class Min extends Constraint { diff --git a/src/Symfony/Component/Validator/Constraints/MinValidator.php b/src/Symfony/Component/Validator/Constraints/MinValidator.php index a91d8738640f..c907793c62b5 100644 --- a/src/Symfony/Component/Validator/Constraints/MinValidator.php +++ b/src/Symfony/Component/Validator/Constraints/MinValidator.php @@ -18,6 +18,8 @@ * @author Bernhard Schussek * * @api + * + * @deprecated Deprecated since version 2.1, to be removed in 2.3. */ class MinValidator extends ConstraintValidator { diff --git a/src/Symfony/Component/Validator/Constraints/Range.php b/src/Symfony/Component/Validator/Constraints/Range.php index 0b4ec8f92f59..20039f5972c4 100644 --- a/src/Symfony/Component/Validator/Constraints/Range.php +++ b/src/Symfony/Component/Validator/Constraints/Range.php @@ -12,6 +12,7 @@ namespace Symfony\Component\Validator\Constraints; use Symfony\Component\Validator\Constraint; +use Symfony\Component\Validator\Exception\MissingOptionsException; /** * @Annotation @@ -26,11 +27,12 @@ class Range extends Constraint public $min; public $max; - /** - * {@inheritDoc} - */ - public function getRequiredOptions() + public function __construct($options = null) { - return array('min', 'max'); + parent::__construct($options); + + if (null === $this->min && null === $this->max) { + throw new MissingOptionsException('Either option "min" or "max" must be given for constraint ' . __CLASS__, array('min', 'max')); + } } } diff --git a/src/Symfony/Component/Validator/Constraints/RangeValidator.php b/src/Symfony/Component/Validator/Constraints/RangeValidator.php index 30b6dbcc6bfe..4c30388c86e2 100644 --- a/src/Symfony/Component/Validator/Constraints/RangeValidator.php +++ b/src/Symfony/Component/Validator/Constraints/RangeValidator.php @@ -41,7 +41,7 @@ public function validate($value, Constraint $constraint) return; } - if ($value > $constraint->max) { + if (null !== $constraint->max && $value > $constraint->max) { $this->context->addViolation($constraint->maxMessage, array( '{{ value }}' => $value, '{{ limit }}' => $constraint->max, @@ -50,7 +50,7 @@ public function validate($value, Constraint $constraint) return; } - if ($value < $constraint->min) { + if (null !== $constraint->min && $value < $constraint->min) { $this->context->addViolation($constraint->minMessage, array( '{{ value }}' => $value, '{{ limit }}' => $constraint->min, diff --git a/src/Symfony/Component/Validator/Tests/Constraints/RangeValidatorTest.php b/src/Symfony/Component/Validator/Tests/Constraints/RangeValidatorTest.php index 2b202782eb3e..c44b0ea6ce43 100644 --- a/src/Symfony/Component/Validator/Tests/Constraints/RangeValidatorTest.php +++ b/src/Symfony/Component/Validator/Tests/Constraints/RangeValidatorTest.php @@ -34,19 +34,7 @@ public function testNullIsValid() $this->validator->validate(null, new Range(array('min' => 10, 'max' => 20))); } - /** - * @dataProvider getValidValues - */ - public function testValidValues($value) - { - $this->context->expects($this->never()) - ->method('addViolation'); - - $constraint = new Range(array('min' => 10, 'max' => 20)); - $this->validator->validate($value, $constraint); - } - - public function getValidValues() + public function getTenToTwenty() { return array( array(10.00001), @@ -60,18 +48,146 @@ public function getValidValues() ); } + public function getLessThanTen() + { + return array( + array(9.99999), + array('9.99999'), + array(5), + array(1.0), + ); + } + + public function getMoreThanTwenty() + { + return array( + array(20.000001), + array('20.000001'), + array(21), + array(30.0), + ); + } + /** - * @dataProvider getInvalidValues + * @dataProvider getTenToTwenty */ - public function testInvalidValues($value) + public function testValidValuesMin($value) { - $this->context->expects($this->once()) + $this->context->expects($this->never()) + ->method('addViolation'); + + $constraint = new Range(array('min' => 10)); + $this->validator->validate($value, $constraint); + } + + /** + * @dataProvider getTenToTwenty + */ + public function testValidValuesMax($value) + { + $this->context->expects($this->never()) + ->method('addViolation'); + + $constraint = new Range(array('max' => 20)); + $this->validator->validate($value, $constraint); + } + + /** + * @dataProvider getTenToTwenty + */ + public function testValidValuesMinMax($value) + { + $this->context->expects($this->never()) ->method('addViolation'); $constraint = new Range(array('min' => 10, 'max' => 20)); $this->validator->validate($value, $constraint); } + /** + * @dataProvider getLessThanTen + */ + public function testInvalidValuesMin($value) + { + $constraint = new Range(array( + 'min' => 10, + 'minMessage' => 'myMessage', + )); + + $this->context->expects($this->once()) + ->method('addViolation') + ->with('myMessage', $this->identicalTo(array( + '{{ value }}' => $value, + '{{ limit }}' => 10, + ))); + + $this->validator->validate($value, $constraint); + } + + /** + * @dataProvider getMoreThanTwenty + */ + public function testInvalidValuesMax($value) + { + $constraint = new Range(array( + 'max' => 20, + 'maxMessage' => 'myMessage', + )); + + $this->context->expects($this->once()) + ->method('addViolation') + ->with('myMessage', $this->identicalTo(array( + '{{ value }}' => $value, + '{{ limit }}' => 20, + ))); + + $this->validator->validate($value, $constraint); + } + + /** + * @dataProvider getMoreThanTwenty + */ + public function testInvalidValuesCombinedMax($value) + { + $constraint = new Range(array( + 'min' => 10, + 'max' => 20, + 'minMessage' => 'myMinMessage', + 'maxMessage' => 'myMaxMessage', + )); + + $this->context->expects($this->once()) + ->method('addViolation') + ->with('myMaxMessage', $this->identicalTo(array( + '{{ value }}' => $value, + '{{ limit }}' => 20, + ))); + + $this->validator->validate($value, $constraint); + } + + /** + * @dataProvider getLessThanTen + */ + public function testInvalidValuesCombinedMin($value) + { + $constraint = new Range(array( + 'min' => 10, + 'max' => 20, + 'minMessage' => 'myMinMessage', + 'maxMessage' => 'myMaxMessage', + )); + + $this->context->expects($this->once()) + ->method('addViolation') + ->with('myMinMessage', $this->identicalTo(array( + '{{ value }}' => $value, + '{{ limit }}' => 10, + ))); + + $this->validator->validate($value, $constraint); + } + public function getInvalidValues() { return array(