8000 merged branch bschussek/range_count_length (PR #4863) · sigues/symfony@b3d1958 · GitHub
[go: up one dir, main page]

Skip to content

Commit b3d1958

Browse files
committed
merged branch bschussek/range_count_length (PR symfony#4863)
Commits ------- a92f80b [Validator] Added Length constraint and deprecated MinLength and MaxLength 83a3f75 [Validator] Deprecated the constraints Min and Max in favor of Range 0cdacee [Validator] Removed MinCount and MaxCount and replaced them by the constraint Count 741c147 [Validator] Renamed deprecated Size constraint to Range Discussion ---------- [Validator] Reintroduced Range constraint and created Count and Length constraints Bug fix: no Feature addition: no Backwards compatibility break: no Symfony2 tests pass: yes Fixes the following tickets: - Todo: - After @Tobion's comment to symfony#4851, this is the next try to streamline the constraints and reduce duplication of logic. The downside of the current MinLength/MaxLength and MinCount/MaxCount pairs is that they cannot output a fitting error message if a value should have an *exact* length/count. So this PR introduces * Range (formerly Size) to replace Min/Max * Count to replace MinCount/MaxCount * Length to replace MinLength/MaxLength Feedback is appreciated. --------------------------------------------------------------------------- by Tobion at 2012-07-11T20:40:08Z The `choice` constraint also cannot handle `min = max`. Or maybe we don't need these options on choice anymore as we can achieve the same with the new `count` constraint?! --------------------------------------------------------------------------- by beberlei at 2012-07-12T08:59:44Z Dude, nobody has time to fix the BC breaks you introduce :-) --------------------------------------------------------------------------- by TomAdam at 2012-07-12T12:38:49Z The changes to the `Size` validator yesterday broke my project, and I started rewriting to use `MaxLength / MinLength` validators today, until I spotted this. It would be good if this PR could have a reasonably high priority (whether or not it is accepted) as it will change how I fix my issues. I suspect a lot of people using the master branch will be in the same situation.
2 parents 1484ca7 + a92f80b commit b3d1958

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+1054
-610
lines changed

UPGRADE-2.1.md

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1136,8 +1136,8 @@
11361136
private $recursiveCollection;
11371137
```
11381138
1139-
* The `Size` constraint was deprecated and will be removed in Symfony 2.3. You should
1140-
use the constraints `Min` and `Max` instead.
1139+
* The `Size`, `Min` and `Max` constraints were deprecated and will be removed in
1140+
Symfony 2.3. You should use the new constraint `Range` instead.
11411141
11421142
Before:
11431143
@@ -1149,13 +1149,41 @@
11491149
After:
11501150
11511151
```
1152-
/**
1153-
* @Assert\Min(2)
1154-
* @Assert\Max(16)
1155-
*/
1152+
/** @Assert\Range(min = 2, max = 16) */
11561153
private $numberOfCpus;
11571154
```
11581155
1156+
Before:
1157+
1158+
```
1159+
/** @Assert\Min(2) */
1160+
private $numberOfCpus;
1161+
```
1162+
1163+
After:
1164+
1165+
```
1166+
/** @Assert\Range(min = 2) */
1167+
private $numberOfCpus;
1168+
```
1169+
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+
11591187
### Session
11601188
11611189
* Flash messages now return an array based on their type. The old method is

src/Symfony/Component/Validator/CHANGELOG.md

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,5 +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 MinCount and MaxCount constraint
25-
* deprecated the Size constraint
24+
* added Count constraint
25+
* added Length constraint
26+
* deprecated the Size constraint and renamed it to Range
27+
* deprecated the Min and Max constraints
28+
* deprecated the MinLength and MaxLength constraints
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
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 10000 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 Count extends Constraint
23+
{
24+
public $minMessage = 'This collection should contain {{ limit }} elements or more.';
25+
public $maxMessage = 'This collection should contain {{ limit }} elements or less.';
26+
public $exactMessage = 'This collection should contain exactly {{ limit }} elements.';
27+
public $min;
28+
public $max;
29+
30+
public function __construct($options = null)
31+
{
32+
if (null !== $options && !is_array($options)) {
33+
$options = array(
34+
'min' => $options,
35+
'max' => $options,
36+
);
37+
}
38+
39+
parent::__construct($options);
40+
41+
if (null === $this->min && null === $this->max) {
42+
throw new MissingOptionsException('Either option "min" or "max" must be given for constraint ' . __CLASS__, array('min', 'max'));
43+
}
44+
}
45+
}

src/Symfony/Component/Validator/Constraints/MaxCountValidator.php renamed to src/Symfony/Component/Validator/Constraints/CountValidator.php

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,13 @@
1818
/**
1919
* @author Bernhard Schussek <bschussek@gmail.com>
2020
*/
21-
class MaxCountValidator extends ConstraintValidator
21+
class CountValidator extends ConstraintValidator
2222
{
2323
/**
2424
* Checks if the passed value is valid.
2525
*
2626
* @param mixed $value The value that should be validated
2727
* @param Constraint $constraint The constraint for the validation
28-
*
29-
* @api
3028
*/
3129
public function validate($value, Constraint $constraint)
3230
{
@@ -40,11 +38,29 @@ public function validate($value, Constraint $constraint)
4038

4139
$count = count($value);
4240

43-
if ($count > $constraint->limit) {
44-
$this->context->addViolation($constraint->message, array(
41+
if ($constraint->min == $constraint->max && $count != $constraint->min) {
42+
$this->context->addViolation($constraint->exactMessage, array(
43+
'{{ count }}' => $count,
44+
'{{ limit }}' => $constraint->min,
45+
), $value, (int) $constraint->min);
46+
47+
return;
48+
}
49+
50+
if (null !== $constraint->max && $count > $constraint->max) {
51+
$this->context->addViolation($constraint->maxMessage, array(
52+
'{{ count }}' => $count,
53+
'{{ limit }}' => $constraint->max,
54+
), $value, (int) $constraint->max);
55+
56+
return;
57+
}
58+
59+
if (null !== $constraint->min && $count < $constraint->min) {
60+
$this->context->addViolation($constraint->minMessage, array(
4561
'{{ count }}' => $count,
46-
'{{ limit }}' => $constraint->limit,
47-
), $value, (int) $constraint->limit);
62+
'{{ limit }}' => $constraint->min,
63+
), $value, (int) $constraint->min);
4864
}
4965< F440 /code>
}
5066
}
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+
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/Max.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 Max extends Constraint
2224
{

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

Lines changed: 0 additions & 41 deletions
This file was deleted.

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/MaxValidator.php

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

src/Symfony/Component/Validator/Constraints/Min.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 Min extends Constraint
2224
{

0 commit comments

Comments
 (0)
0