added a Size validator

This commit is contained in:
Fabien Potencier 2011-09-29 15:56:37 +02:00
parent b9ba117208
commit d6c4bfb001
4 changed files with 200 additions and 0 deletions

View File

@ -77,6 +77,7 @@ To get the diff between two versions, go to https://github.com/symfony/symfony/c
### Validator
* added a Size validator
* added a SizeLength validator
* improved the ImageValidator with min width, max width, min height, and max height constraints
* added support for MIME with wildcard in FileValidator

View File

@ -0,0 +1,36 @@
<?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 Size 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');
}
}

View File

@ -0,0 +1,66 @@
<?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;
/**
* @api
*/
class SizeValidator 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 isValid($value, Constraint $constraint)
{
if (null === $value) {
return true;
}
if (!is_numeric($value)) {
$this->setMessage($constraint->invalidMessage, array(
'{{ value }}' => $value,
));
return false;
}
if ($value > $constraint->max) {
$this->setMessage($constraint->maxMessage, array(
'{{ value }}' => $value,
'{{ limit }}' => $constraint->max,
));
return false;
}
if ($value < $constraint->min) {
$this->setMessage($constraint->minMessage, array(
'{{ value }}' => $value,
'{{ limit }}' => $constraint->min,
));
return false;
}
return true;
}
}

View File

@ -0,0 +1,97 @@
<?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\Tests\Component\Validator\Constraints;
use Symfony\Component\Validator\Constraints\Size;
use Symfony\Component\Validator\Constraints\SizeValidator;
class SizeValidatorTest extends \PHPUnit_Framework_TestCase
{
protected $validator;
protected function setUp()
{
$this->validator = new SizeValidator();
}
public function testNullIsValid()
{
$this->assertTrue($this->validator->isValid(null, new Size(array('min' => 10, 'max' => 20))));
}
/**
* @dataProvider getValidValues
*/
public function testValidValues($value)
{
$constraint = new Size(array('min' => 10, 'max' => 20));
$this->assertTrue($this->validator->isValid($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)
{
$constraint = new Size(array('min' => 10, 'max' => 20));
$this->assertFalse($this->validator->isValid($value, $constraint));
}
public function getInvalidValues()
{
return array(
array(9.999999),
array(20.000001),
array('9.999999'),
array('20.000001'),
array(new \stdClass()),
);
}
public function testMessageIsSet()
{
$constraint = new Size(array(
'min' => 10,
'max' => 20,
'minMessage' => 'myMinMessage',
'maxMessage' => 'myMaxMessage',
));
$this->assertFalse($this->validator->isValid(9, $constraint));
$this->assertEquals('myMinMessage', $this->validator->getMessageTemplate());
$this->assertEquals(array(
'{{ value }}' => 9,
'{{ limit }}' => 10,
), $this->validator->getMessageParameters());
$this->assertFalse($this->validator->isValid(21, $constraint));
$this->assertEquals('myMaxMessage', $this->validator->getMessageTemplate());
$this->assertEquals(array(
'{{ value }}' => 21,
'{{ limit }}' => 20,
), $this->validator->getMessageParameters());
}
}