From d6c4bfb001f5f3c3847c3f7766467d16f7e8e322 Mon Sep 17 00:00:00 2001 From: Fabien Potencier Date: Thu, 29 Sep 2011 15:56:37 +0200 Subject: [PATCH] added a Size validator --- CHANGELOG-2.1.md | 1 + .../Component/Validator/Constraints/Size.php | 36 +++++++ .../Validator/Constraints/SizeValidator.php | 66 +++++++++++++ .../Constraints/SizeValidatorTest.php | 97 +++++++++++++++++++ 4 files changed, 200 insertions(+) create mode 100644 src/Symfony/Component/Validator/Constraints/Size.php create mode 100644 src/Symfony/Component/Validator/Constraints/SizeValidator.php create mode 100644 tests/Symfony/Tests/Component/Validator/Constraints/SizeValidatorTest.php diff --git a/CHANGELOG-2.1.md b/CHANGELOG-2.1.md index 5edc90ce8e..7408adb52e 100644 --- a/CHANGELOG-2.1.md +++ b/CHANGELOG-2.1.md @@ -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 diff --git a/src/Symfony/Component/Validator/Constraints/Size.php b/src/Symfony/Component/Validator/Constraints/Size.php new file mode 100644 index 0000000000..d350c78052 --- /dev/null +++ b/src/Symfony/Component/Validator/Constraints/Size.php @@ -0,0 +1,36 @@ + + * + * 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'); + } +} diff --git a/src/Symfony/Component/Validator/Constraints/SizeValidator.php b/src/Symfony/Component/Validator/Constraints/SizeValidator.php new file mode 100644 index 0000000000..51fa5fb962 --- /dev/null +++ b/src/Symfony/Component/Validator/Constraints/SizeValidator.php @@ -0,0 +1,66 @@ + + * + * 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; + } +} diff --git a/tests/Symfony/Tests/Component/Validator/Constraints/SizeValidatorTest.php b/tests/Symfony/Tests/Component/Validator/Constraints/SizeValidatorTest.php new file mode 100644 index 0000000000..49c39f37f4 --- /dev/null +++ b/tests/Symfony/Tests/Component/Validator/Constraints/SizeValidatorTest.php @@ -0,0 +1,97 @@ + + * + * 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()); + } +}