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
Next Next commit
[Validator] Renamed deprecated Size constraint to Range
  • Loading branch information
webmozart committed Jul 11, 2012
commit 741c147ce55b40231e71d7ab14e96774be98f376
2 changes: 1 addition & 1 deletion src/Symfony/Component/Validator/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,4 @@ CHANGELOG
recursively anymore by default. `Valid` contains a new property `deep`
which enables the BC behavior.
* added MinCount and MaxCount constraint
* deprecated the Size constraint
* deprecated the Size constraint and renamed it to Range
36 changes: 36 additions & 0 deletions src/Symfony/Component/Validator/Constraints/Range.php
Original file line number Diff line number Diff line change
@@ -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 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';
Copy link
Contributor

Choose a reason for hiding this comment

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

this constraint doesnt support the exact number?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

no

public $min;
public $max;

/**
* {@inheritDoc}
*/
public function getRequiredOptions()
{
return array('min', 'max');
}
}
60 changes: 60 additions & 0 deletions src/Symfony/Component/Validator/Constraints/RangeValidator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?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>
*/
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
*/
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,
));
}
}
}
12 changes: 3 additions & 9 deletions src/Symfony/Component/Validator/Constraints/Size.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,13 @@
*
* @deprecated Deprecated since version 2.1, to be removed in 2.3.
*/
class Size extends Constraint
class Size extends Range
{
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()
public function validatedBy()
{
return array('min', 'max');
return get_parent_class($this).'Validator';
}
}
42 changes: 1 addition & 41 deletions src/Symfony/Component/Validator/Constraints/SizeValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,46 +21,6 @@
*
* @deprecated Deprecated since version 2.1, to be removed in 2.3.
*/
class SizeValidator extends ConstraintValidator
class SizeValidator extends RangeValidator
{
/**
* 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,
));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,18 @@

namespace Symfony\Component\Validator\Tests\Constraints;

use Symfony\Component\Validator\Constraints\Size;
use Symfony\Component\Validator\Constraints\SizeValidator;
use Symfony\Component\Validator\Constraints\Range;
use Symfony\Component\Validator\Constraints\RangeValidator;

class SizeValidatorTest extends \PHPUnit_Framework_TestCase
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 SizeValidator();
$this->validator = new RangeValidator();
$this->validator->initialize($this->context);
}

Expand All @@ -31,7 +31,7 @@ public function testNullIsValid()
$this->context->expects($this->never())
->method('addViolation');

$this->validator->validate(null, new Size(array('min' => 10, 'max' => 20)));
$this->validator->validate(null, new Range(array('min' => 10, 'max' => 20)));
}

/**
Expand All @@ -42,7 +42,7 @@ public function testValidValues($value)
$this->context->expects($this->never())
->method('addViolation');

$constraint = new Size(array('min' => 10, 'max' => 20));
$constraint = new Range(array('min' => 10, 'max' => 20));
$this->validator->validate($value, $constraint);
}

Expand All @@ -68,7 +68,7 @@ public function testInvalidValues($value)
$this->context->expects($this->once())
->method('addViolation');

$constraint = new Size(array('min' => 10, 'max' => 20));
$constraint = new Range(array('min' => 10, 'max' => 20));
$this->validator->validate($value, $constraint);
}

Expand All @@ -85,7 +85,7 @@ public function getInvalidValues()

public function testMinMessageIsSet()
{
$constraint = new Size(array(
$constraint = new Range(array(
'min' => 10,
'max' => 20,
'minMessage' => 'myMessage',
Expand All @@ -103,7 +103,7 @@ public function testMinMessageIsSet()

public function testMaxMessageIsSet()
{
$constraint = new Size(array(
$constraint = new Range(array(
'min' => 10,
'max' => 20,
'maxMessage' => 'myMessage',
Expand Down
0