Skip to content

Commit

Permalink
[Validator] added a SizeLength validator
Browse files Browse the repository at this point in the history
  • Loading branch information
fabpot committed Sep 29, 2011
1 parent 5306ca8 commit b9ba117
Show file tree
Hide file tree
Showing 4 changed files with 250 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG-2.1.md
Expand Up @@ -77,5 +77,6 @@ To get the diff between two versions, go to https://github.com/symfony/symfony/c

### 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
37 changes: 37 additions & 0 deletions src/Symfony/Component/Validator/Constraints/SizeLength.php
@@ -0,0 +1,37 @@
<?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 SizeLength extends Constraint
{
public $minMessage = 'This value is too short. It should have {{ limit }} characters or more';
public $maxMessage = 'This value is too long. It should have {{ limit }} characters or less';
public $exactMessage = 'This value should have exactly {{ limit }} characters';
public $min;
public $max;
public $charset = 'UTF-8';

/**
* {@inheritDoc}
*/
public function getRequiredOptions()
{
return array('min', 'max');
}
}
@@ -0,0 +1,76 @@
<?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;
use Symfony\Component\Validator\Exception\UnexpectedTypeException;

/**
* @api
*/
class SizeLengthValidator 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 || '' === $value) {
return true;
}

if (!is_scalar($value) && !(is_object($value) && method_exists($value, '__toString'))) {
throw new UnexpectedTypeException($value, 'string');
}

$value = (string) $value;

$length = function_exists('mb_strlen') ? mb_strlen($value, $constraint->charset) : strlen($value);

if ($constraint->min == $constraint->max && $length != $constraint->max) {
$this->setMessage($constraint->exactMessage, array(
'{{ value }}' => $value,
'{{ limit }}' => $constraint->max,
));

return false;
}

if ($length > $constraint->max) {
$this->setMessage($constraint->maxMessage, array(
'{{ value }}' => $value,
'{{ limit }}' => $constraint->max,
));

return false;
}

if ($length < $constraint->min) {
$this->setMessage($constraint->minMessage, array(
'{{ value }}' => $value,
'{{ limit }}' => $constraint->min,
));

return false;
}

return true;
}
}
@@ -0,0 +1,136 @@
<?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\SizeLength;
use Symfony\Component\Validator\Constraints\SizeLengthValidator;

class SizeLengthValidatorTest extends \PHPUnit_Framework_TestCase
{
protected $validator;

protected function setUp()
{
$this->validator = new SizeLengthValidator();
}

protected function tearDown()
{
$this->validator = null;
}

public function testNullIsValid()
{
$this->assertTrue($this->validator->isValid(null, new SizeLength(array('min' => 6, 'max' => 10))));
}

public function testEmptyStringIsValid()
{
$this->assertTrue($this->validator->isValid('', new SizeLength(array('min' => 6, 'max' => 10))));
}

public function testExpectsStringCompatibleType()
{
$this->setExpectedException('Symfony\Component\Validator\Exception\UnexpectedTypeException');

$this->validator->isValid(new \stdClass(), new SizeLength(array('min' => 6, 'max' => 10)));
}

/**
* @dataProvider getValidValues
*/
public function testValidValues($value, $skip = false)
{
if (!$skip) {
$constraint = new SizeLength(array('min' => 6, 'max' => 10));
$this->assertTrue($this->validator->isValid($value, $constraint));
}
}

public function getValidValues()
{
return array(
array(123456),
array(1234567890),
array('123456'),
array('1234567890'),
array('üüüüüü', !function_exists('mb_strlen')),
array('üüüüüüüüüü', !function_exists('mb_strlen')),
array('éééééé', !function_exists('mb_strlen')),
array('éééééééééé', !function_exists('mb_strlen')),
);
}

/**
* @dataProvider getInvalidValues
*/
public function testInvalidValues($value, $skip = false)
{
if (!$skip) {
$constraint = new SizeLength(array('min' => 6, 'max' => 10));
$this->assertFalse($this->validator->isValid($value, $constraint));
}
}

public function getInvalidValues()
{
return array(
array(12345),
array(12345678901),
array('12345'),
array('12345678901'),
array('üüüüü', !function_exists('mb_strlen')),
array('üüüüüüüüüüü', !function_exists('mb_strlen')),
array('ééééé', !function_exists('mb_strlen')),
array('ééééééééééé', !function_exists('mb_strlen')),
);
}

public function testMessageIsSet()
{
$constraint = new SizeLength(array(
'min' => 5,
'max' => 10,
'minMessage' => 'myMinMessage',
'maxMessage' => 'myMaxMessage',
));

$this->assertFalse($this->validator->isValid('1234', $constraint));
$this->assertEquals('myMinMessage', $this->validator->getMessageTemplate());
$this->assertEquals(array(
'{{ value }}' => '1234',
'{{ limit }}' => 5,
), $this->validator->getMessageParameters());

$this->assertFalse($this->validator->isValid('12345678901', $constraint));
$this->assertEquals('myMaxMessage', $this->validator->getMessageTemplate());
$this->assertEquals(array(
'{{ value }}' => '12345678901',
'{{ limit }}' => 10,
), $this->validator->getMessageParameters());
}

public function testExactErrorMessage()
{
$constraint = new SizeLength(array(
'min' => 5,
'max' => 5,
));

$this->assertFalse($this->validator->isValid('1234', $constraint));
$this->assertEquals('This value should have exactly {{ limit }} characters', $this->validator->getMessageTemplate());
$this->assertEquals(array(
'{{ value }}' => '1234',
'{{ limit }}' => 5,
), $this->validator->getMessageParameters());
}
}

1 comment on commit b9ba117

@jmikola
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This reminds me of another useful validator that I previously had to create myself. What are you thoughts on enhancing this to also support arrays and Countable objects? Then it could also be used on collections?

Previously, the developer's two options for such validation was to either roll their own validator or use the Min/Max numeric validators on a method returning the count (possible for object validation, not so much for arrays).

Please sign in to comment.