[Validator] Removed the Range constraint as it duplicates functionality given in Min and Max
This commit is contained in:
parent
09d71ca927
commit
1a732e4983
@ -167,7 +167,6 @@ class ValidatorTypeGuesser implements FormTypeGuesserInterface
|
||||
break;
|
||||
|
||||
case 'Symfony\Component\Validator\Constraints\Min':
|
||||
case 'Symfony\Component\Validator\Constraints\Range':
|
||||
case 'Symfony\Component\Validator\Constraints\Max':
|
||||
return new TypeGuess('number', array(), Guess::LOW_CONFIDENCE);
|
||||
|
||||
@ -222,9 +221,6 @@ class ValidatorTypeGuesser implements FormTypeGuesserInterface
|
||||
|
||||
case 'Symfony\Component\Validator\Constraints\Max':
|
||||
return new ValueGuess(strlen((string) $constraint->limit), Guess::LOW_CONFIDENCE);
|
||||
|
||||
case 'Symfony\Component\Validator\Constraints\Range':
|
||||
return new ValueGuess(strlen((string) $constraint->max), Guess::LOW_CONFIDENCE);
|
||||
}
|
||||
}
|
||||
|
||||
@ -271,9 +267,6 @@ class ValidatorTypeGuesser implements FormTypeGuesserInterface
|
||||
case 'Symfony\Component\Validator\Constraints\Min':
|
||||
return new ValueGuess(sprintf('.{%s,}', strlen((string) $constraint->limit)), Guess::LOW_CONFIDENCE);
|
||||
|
||||
case 'Symfony\Component\Validator\Constraints\Range':
|
||||
return new ValueGuess(sprintf('.{%s,%s}', strlen((string) $constraint->min), strlen((string) $constraint->max)), Guess::LOW_CONFIDENCE);
|
||||
|
||||
case 'Symfony\Component\Validator\Constraints\Type':
|
||||
if (in_array($constraint->type, array('double', 'float', 'numeric', 'real'))) {
|
||||
return new ValueGuess(null, Guess::MEDIUM_CONFIDENCE);
|
||||
|
@ -5,7 +5,6 @@ CHANGELOG
|
||||
-----
|
||||
|
||||
* added support for `ctype_*` assertions in `TypeValidator`
|
||||
* added a Range validator for numeric values
|
||||
* added a Size validator for string & collections
|
||||
* improved the ImageValidator with min width, max width, min height, and max height constraints
|
||||
* added support for MIME with wildcard in FileValidator
|
||||
|
@ -1,36 +0,0 @@
|
||||
<?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 Range extends Constraint
|
||||
{
|
||||
public $minMessage = 'This value should be {{ limit }} or more.';
|
||||
public $maxMessage = 'This value should be {{ limit }} or less.';
|
||||
public $invalidMessage = 'This value should be a valid number.';
|
||||
public $min;
|
||||
public $max;
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
public function getRequiredOptions()
|
||||
{
|
||||
return array('min', 'max');
|
||||
}
|
||||
}
|
@ -1,64 +0,0 @@
|
||||
<?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;
|
||||
|
||||
/**
|
||||
* @author Bernhard Schussek <bschussek@gmail.com>
|
||||
*
|
||||
* @api
|
||||
*/
|
||||
class RangeValidator extends ConstraintValidator
|
||||
{
|
||||
/**
|
||||
* Checks if the passed value is valid.
|
||||
*
|
||||
* @param mixed $value The value that should be validated
|
||||
* @param Constraint $constraint The constraint for the validation
|
||||
*
|
||||
* @return Boolean Whether or not the value is valid
|
||||
*
|
||||
* @api
|
||||
*/
|
||||
public function validate($value, Constraint $constraint)
|
||||
{
|
||||
if (null === $value) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (!is_numeric($value)) {
|
||||
$this->context->addViolation($constraint->invalidMessage, array(
|
||||
'{{ value }}' => $value,
|
||||
));
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
if ($value > $constraint->max) {
|
||||
$this->context->addViolation($constraint->maxMessage, array(
|
||||
'{{ value }}' => $value,
|
||||
'{{ limit }}' => $constraint->max,
|
||||
));
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
if ($value < $constraint->min) {
|
||||
$this->context->addViolation($constraint->minMessage, array(
|
||||
'{{ value }}' => $value,
|
||||
'{{ limit }}' => $constraint->min,
|
||||
));
|
||||
}
|
||||
}
|
||||
}
|
@ -1,121 +0,0 @@
|
||||
<?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\Tests\Constraints;
|
||||
|
||||
use Symfony\Component\Validator\Constraints\Range;
|
||||
use Symfony\Component\Validator\Constraints\RangeValidator;
|
||||
|
||||
class RangeValidatorTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
protected $context;
|
||||
protected $validator;
|
||||
|
||||
protected function setUp()
|
||||
{
|
||||
$this->context = $this->getMock('Symfony\Component\Validator\ExecutionContext', array(), array(), '', false);
|
||||
$this->validator = new RangeValidator();
|
||||
$this->validator->initialize($this->context);
|
||||
}
|
||||
|
||||
public function testNullIsValid()
|
||||
{
|
||||
$this->context->expects($this->never())
|
||||
->method('addViolation');
|
||||
|
||||
$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()
|
||||
{
|
||||
return array(
|
||||
array(10.00001),
|
||||
array(19.99999),
|
||||
array('10.00001'),
|
||||
array('19.99999'),
|
||||
array(10),
|
||||
array(20),
|
||||
array(10.0),
|
||||
array(20.0),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider getInvalidValues
|
||||
*/
|
||||
public function testInvalidValues($value)
|
||||
{
|
||||
$this->context->expects($this->once())
|
||||
->method('addViolation');
|
||||
|
||||
$constraint = new Range(array('min' => 10, 'max' => 20));
|
||||
$this->validator->validate($value, $constraint);
|
||||
}
|
||||
|
||||
public function getInvalidValues()
|
||||
{
|
||||
return array(
|
||||
array(9.999999),
|
||||
array(20.000001),
|
||||
array('9.999999'),
|
||||
array('20.000001'),
|
||||
array(new \stdClass()),
|
||||
);
|
||||
}
|
||||
|
||||
public function testMinMessageIsSet()
|
||||
{
|
||||
$constraint = new Range(array(
|
||||
'min' => 10,
|
||||
'max' => 20,
|
||||
'minMessage' => 'myMessage',
|
||||
));
|
||||
|
||||
$this->context->expects($this->once())
|
||||
->method('addViolation')
|
||||
->with('myMessage', array(
|
||||
'{{ value }}' => 9,
|
||||
'{{ limit }}' => 10,
|
||||
));
|
||||
|
||||
$this->validator->validate(9, $constraint);
|
||||
}
|
||||
|
||||
public function testMaxMessageIsSet()
|
||||
{
|
||||
$constraint = new Range(array(
|
||||
'min' => 10,
|
||||
'max' => 20,
|
||||
'maxMessage' => 'myMessage',
|
||||
));
|
||||
|
||||
$this->context->expects($this->once())
|
||||
->method('addViolation')
|
||||
->with('myMessage', array(
|
||||
'{{ value }}' => 21,
|
||||
'{{ limit }}' => 20,
|
||||
));
|
||||
|
||||
$this->validator->validate(21, $constraint);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user