Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Validator] Renamed deprecated Size constraint to Range
- Loading branch information
Showing
6 changed files
with
110 additions
and
60 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'; | ||
public $min; | ||
public $max; | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
public function getRequiredOptions() | ||
{ | ||
return array('min', 'max'); | ||
} | ||
} |
60 changes: 60 additions & 0 deletions
60
src/Symfony/Component/Validator/Constraints/RangeValidator.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
)); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters