8000 [Validator] Reintroduced Range constraint and created Count and Length constraints by webmozart · Pull Request #4863 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Validator] Reintroduced Range constraint and created Count and Length constraints #4863

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

Merged
merged 4 commits into from
Jul 12, 2012
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
[Validator] Deprecated the constraints Min and Max in favor of Range
  • Loading branch information
webmozart committed Jul 11, 2012
commit 83a3f75b2dd6619b0ed806404f9164a8c7b7a5f1
23 changes: 17 additions & 6 deletions UPGRADE-2.1.md
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
* @author Bernhard Schussek 10000 <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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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'));
}
}
}
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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( B678
'{{ 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