Skip to content

Commit

Permalink
[Validator] Deprecated the constraints Min and Max in favor of Range
Browse files Browse the repository at this point in the history
  • Loading branch information
webmozart committed Jul 11, 2012
1 parent 0cdacee commit 83a3f75
Show file tree
Hide file tree
Showing 9 changed files with 167 additions and 29 deletions.
23 changes: 17 additions & 6 deletions UPGRADE-2.1.md
Expand Up @@ -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:

Expand All @@ -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;
```

Expand Down
1 change: 1 addition & 0 deletions src/Symfony/Component/Validator/CHANGELOG.md
Expand Up @@ -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
2 changes: 2 additions & 0 deletions src/Symfony/Component/Validator/Constraints/Max.php
Expand Up @@ -17,6 +17,8 @@
* @Annotation
*
* @api
*
* @deprecated Deprecated since version 2.1, to be removed in 2.3.
*/
class Max extends Constraint
{
Expand Down
2 changes: 2 additions & 0 deletions src/Symfony/Component/Validator/Constraints/MaxValidator.php
Expand Up @@ -18,6 +18,8 @@
* @author Bernhard Schussek <bschussek@gmail.com>
*
* @api
*
* @deprecated Deprecated since version 2.1, to be removed in 2.3.
*/
class MaxValidator extends ConstraintValidator
{
Expand Down
2 changes: 2 additions & 0 deletions src/Symfony/Component/Validator/Constraints/Min.php
Expand Up @@ -17,6 +17,8 @@
* @Annotation
*
* @api
*
* @deprecated Deprecated since version 2.1, to be removed in 2.3.
*/
class Min extends Constraint
{
Expand Down
2 changes: 2 additions & 0 deletions src/Symfony/Component/Validator/Constraints/MinValidator.php
Expand Up @@ -18,6 +18,8 @@
* @author Bernhard Schussek <bschussek@gmail.com>
*
* @api
*
* @deprecated Deprecated since version 2.1, to be removed in 2.3.
*/
class MinValidator extends ConstraintValidator
{
Expand Down
12 changes: 7 additions & 5 deletions src/Symfony/Component/Validator/Constraints/Range.php
Expand Up @@ -12,6 +12,7 @@
namespace Symfony\Component\Validator\Constraints;

use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\Exception\MissingOptionsException;

/**
* @Annotation
Expand All @@ -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'));
}
}
}
Expand Up @@ -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,
Expand All @@ -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,
Expand Down
148 changes: 132 additions & 16 deletions src/Symfony/Component/Validator/Tests/Constraints/RangeValidatorTest.php
Expand Up @@ -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),
Expand All @@ -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(
Expand Down

0 comments on commit 83a3f75

Please sign in to comment.