10000 [Validator] Added Length constraint and deprecated MinLength and MaxL… · sigues/symfony@a92f80b · GitHub
[go: up one dir, main page]

Skip to content

Commit a92f80b

Browse files
committed
[Validator] Added Length constraint and deprecated MinLength and MaxLength
1 parent 83a3f75 commit a92f80b

File tree

9 files changed

+381
-1
lines changed

9 files changed

+381
-1
lines changed

UPGRADE-2.1.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1167,6 +1167,23 @@
11671167
private $numberOfCpus;
11681168
```
11691169
1170+
* The `MinLength` and `MaxLength` constraints were deprecated and will be
1171+
removed in Symfony 2.3. You should use the new constraint `Length` instead.
1172+
1173+
Before:
1174+
1175+
```
1176+
/** @Assert\MinLength(8) */
1177+
private $password;
1178+
```
1179+
1180+
After:
1181+
1182+
```
1183+
/** @Assert\Length(min = 8) */
1184+
private $password;
1185+
```
1186+
11701187
### Session
11711188
11721189
* Flash messages now return an array based on their type. The old method is

src/Symfony/Component/Validator/CHANGELOG.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ CHANGELOG
2121
* [BC BREAK] collections in fields annotated with `Valid` are not traversed
2222
recursively anymore by default. `Valid` contains a new property `deep`
2323
which enables the BC behavior.
24+
* added Count constraint
25+
* added Length constraint
2426
* deprecated the Size constraint and renamed it to Range
2527
* deprecated the Min and Max constraints
26-
* added Count constraint
28+
* deprecated the MinLength and MaxLength constraints
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Validator\Constraints;
13+
14+
use Symfony\Component\Validator\Constraint;
15+
use Symfony\Component\Validator\Exception\MissingOptionsException;
16+
17+
/**
18+
* @Annotation
19+
*
20+
* @api
21+
*/
22+
class Length extends Constraint
23+
{
24+
public $maxMessage = 'This value is too long. It should have {{ limit }} characters or less.';
25+
public $minMessage = 'This value is too short. It should have {{ limit }} characters or more.';
26+
public $exactMessage = 'This value should have exactly {{ limit }} characters.';
27+
public $max;
28+
public $min;
29+
public $charset = 'UTF-8';
30+
31+
public function __construct($options = null)
32+
{
33+
if (null !== $options && !is_array($options)) {
34+
$options = array(
35+
'min' => $options,
36+
'max' => $options,
37+
);
38+
}
39+
40+
parent::__construct($options);
41+
57AE 42+
if (null === $this->min && null === $this->max) {
43+
throw new MissingOptionsException('Either option "min" or "max" must be given for constraint ' . __CLASS__, array('min', 'max'));
44+
}
45+
}
46+
}
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Validator\Constraints;
13+
14+
use Symfony\Component\Validator\Constraint;
15+
use Symfony\Component\Validator\ConstraintValidator;
16+
use Symfony\Component\Validator\Exception\UnexpectedTypeException;
17+
18+
/**
19+
* @author Bernhard Schussek <bschussek@gmail.com>
20+
*/
21+
class LengthValidator extends ConstraintValidator
22+
{
23+
/**
24+
* Checks if the passed value is valid.
25+
*
26+
* @param mixed $value The value that should be validated
27+
* @param Constraint $constraint The constraint for the validation
28+
*/
29+
public function validate($value, Constraint $constraint)
30+
{
31+
if (null === $value || '' === $value) {
32+
return;
33+
}
34+
35+
if (!is_scalar($value) && !(is_object($value) && method_exists($value, '__toString'))) {
36+
throw new UnexpectedTypeException($value, 'string');
37+
}
38+
39+
$stringValue = (string) $value;
40+
41+
if (function_exists('grapheme_strlen') && 'UTF-8' === $constraint->charset) {
42+
$length = grapheme_strlen($stringValue);
43+
} elseif (function_exists('mb_strlen')) {
44+
$length = mb_strlen($stringValue, $constraint->charset);
45+
} else {
46+
$length = strlen($stringValue);
47+
}
48+
49+
if ($constraint->min == $constraint->max && $length != $constraint->min) {
50+
$this->context->addViolation($constraint->exactMessage, array(
51+
'{{ value }}' => $stringValue,
52+
'{{ limit }}' => $constraint->min,
53+
), $value, (int) $constraint->min);
54+
55+
return;
56+
}
57+
58+
if (null !== $constraint->max && $length > $constraint->max) {
59+
$this->context->addViolation($constraint->maxMessage, array(
60+
'{{ value }}' => $stringValue,
61+
'{{ limit }}' => $constraint->max,
62+
), $value, (int) $constraint->max);
63+
64+
return;
65+
}
66+
67+
if (null !== $constraint->min && $length < $constraint->min) {
68+
$this->context->addViolation($constraint->minMessage, array(
69+
'{{ value }}' => $stringValue,
70+
'{{ limit }}' => $constraint->min,
71+
), $value, (int) $constraint->min);
72+
}
73+
}
74+
}

src/Symfony/Component/Validator/Constraints/MaxLength.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
* @Annotation
1818
*
1919
* @api
20+
*
21+
* @deprecated Deprecated since version 2.1, to be removed in 2.3.
2022
*/
2123
class MaxLength extends Constraint
2224
{

src/Symfony/Component/Validator/Constraints/MaxLengthValidator.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
* @author Bernhard Schussek <bschussek@gmail.com>
2020
*
2121
* @api
22+
*
23+
* @deprecated Deprecated since version 2.1, to be removed in 2.3.
2224
*/
2325
class MaxLengthValidator extends ConstraintValidator
2426
{

src/Symfony/Component/Validator/Constraints/MinLength.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
* @Annotation
1818
*
1919
* @api
20+
*
21+
* @deprecated Deprecated since version 2.1, to be removed in 2.3.
2022
*/
2123
class MinLength extends Constraint
2224
{

src/Symfony/Component/Validator/Constraints/MinLengthValidator.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
* @author Bernhard Schussek <bschussek@gmail.com>
2020
*
2121
* @api
22+
*
23+ * @deprecated Deprecated since version 2.1, to be removed in 2.3.
2224
*/
2325
class MinLengthValidator extends ConstraintValidator
2426
{

0 commit comments

Comments
 (0)
0