8000 merged branch Herzult/feature/collection_size_validator (PR #4149) · symfony/symfony@5314836 · GitHub
[go: up one dir, main page]

Skip to content

Commit 5314836

Browse files
committed
merged branch Herzult/feature/collection_size_validator (PR #4149)
Commits ------- 3a5e84f [Validator] Add CollectionSize constraint Discussion ---------- [Validator] Add CollectionSize constraint Bug fix: no Feature addition: yes Backwards compatibility break: no Symfony2 tests pass: yes Fixes the following tickets: - Todo: - I will also send a PR to the documentation as soon as this one is accepted. --------------------------------------------------------------------------- by bschussek at 2012-04-29T08:24:28Z -1 I dislike the rising amount of very specific constraints in the core. Can't we add this to Size? --------------------------------------------------------------------------- by vicb at 2012-04-29T09:01:39Z @bschussek #3918 implements what you propose but then the messages are not valid any more: ```php <?php 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'; ``` I can imagine 2 solutions: - adding some more message, - rename the `Size` constraint to `Range` and create a new `Size` constraint for arrays / countables. What do you think ? --------------------------------------------------------------------------- by bschussek at 2012-04-29T09:27:53Z I'd prefer the second solution and merge `Size` with `SizeLength` as well. --------------------------------------------------------------------------- by vicb at 2012-04-29T09:34:50Z @bschussek It would make sense. @makasim @Herzult any one of you would like to contribute this (i.e. rename the current Size to Range and create a new Size supporting arrays / countables / strings) ? --------------------------------------------------------------------------- by Herzult at 2012-04-29T14:31:12Z Yep, I'm on it. --------------------------------------------------------------------------- by stof at 2012-04-29T15:22:44Z @Herzult could you take the other comment into account and merge SizeLength into you Size ? --------------------------------------------------------------------------- by vicb at 2012-04-29T15:33:05Z The guessers should also be modified (it might also affect the ODM which is in an other repo, if so it would be good to sync the changes). --------------------------------------------------------------------------- by Herzult at 2012-04-29T16:38:19Z @stof the problem merging SizeLength into Size is that they don't have the same required options & messages. --------------------------------------------------------------------------- by Herzult at 2012-04-29T16:47:40Z And what about renaming Range to Interval and SizeLength to IntervalLength? --------------------------------------------------------------------------- by stof at 2012-04-29T16:54:38Z Well, SizeLength is about matching the length of a string currently. Nothing related to intervals --------------------------------------------------------------------------- by Herzult at 2012-04-29T17:29:40Z Here are the current names: * **Size** for collection (countable) size * **Range** for numbers * **SizeLength** for strings Merging **SizeLength** into **Size** is maybe not appropriate because collections and strings are different things. It'll be hard to find messages that fit both collections and strings. Maybe we had better to find a better name for both. What do you think? About the ValidatorTypeGuesser, I'll update it as soon as we know ow to name the constraints. --------------------------------------------------------------------------- by vicb at 2012-04-29T17:43:01Z Size is a good name for both strings and "collections", could we have two sets of strings and select according to the type ? --------------------------------------------------------------------------- by Herzult at 2012-04-29T22:39:55Z I tried to merge them together, what do you think? --------------------------------------------------------------------------- by vicb at 2012-04-30T06:52:37Z I think your changes are great, may be @bschussek has more feedback. The ValidatorTypeGuesser and the translation are yet to be updated. --------------------------------------------------------------------------- by hhamon at 2012-05-01T12:32:28Z Am I missing something or `SizeLength` for strings is a duplicate for `MinLength` and `MaxLength` constraints? --------------------------------------------------------------------------- by Herzult at 2012-05-02T13:29:36Z Yep, that's true. But the only link between this PR and the SizeLength constraint is that I merged it to the one I introduced. --------------------------------------------------------------------------- by Herzult at 2012-05-07T07:48:01Z @bschussek what do you think? --------------------------------------------------------------------------- by vicb at 2012-05-10T19:51:26Z @Herzult this PR looks good to me, could you update the changelog and update guides, try to factorize the code and squash the commits ? Thanks. --------------------------------------------------------------------------- by travisbot at 2012-05-11T15:42:35Z This pull request [passes](http://travis-ci.org/symfony/symfony/builds/1306112) (merged 8d8e6443 into 4ac3bdd). --------------------------------------------------------------------------- by vicb at 2012-05-11T21:42:21Z * could #4259 be helpful ? * please squash the commits. * please create a PR / issue on [symfony-docs](https://github.com/symfony/symfony-docs) thanks for the updates. --------------------------------------------------------------------------- by travisbot at 2012-05-13T18:38:18Z This pull request [fails](http://travis-ci.org/symfony/symfony/builds/1321123) (merged eeda9044 into 4ac3bdd). --------------------------------------------------------------------------- by travisbot at 2012-05-13T18:45:12Z This pull request [passes](http://travis-ci.org/symfony/symfony/builds/1321146) (merged 491ca19a into 8b54eb5). --------------------------------------------------------------------------- by travisbot at 2012-05-14T11:29:39Z This pull request [passes](http://travis-ci.org/symfony/symfony/builds/1326110) (merged 44865024 into 8b54eb5). --------------------------------------------------------------------------- by vicb at 2012-05-14T11:49:37Z @Herzult what about plural translations ? --------------------------------------------------------------------------- by travisbot at 2012-05-14T16:52:37Z This pull request [passes](http://travis-ci.org/symfony/symfony/builds/1328677) (merged 93480f95 into 46ffbd5). --------------------------------------------------------------------------- by travisbot at 2012-05-14T17:03:13Z This pull request [passes](http://travis-ci.org/symfony/symfony/builds/1328705) (merged 326c3b81 into 46ffbd5). --------------------------------------------------------------------------- by vicb at 2012-05-14T20:19:18Z thanks for the updates, this PR looks fine to me. @bschussek ? --------------------------------------------------------------------------- by vicb at 2012-05-16T06:45:51Z @Herzult can you squash your commits ? --------------------------------------------------------------------------- by travisbot at 2012-05-16T11:20:44Z This pull request [passes](http://travis-ci.org/symfony/symfony/builds/1344811) (merged 3a5e84f into 58b6ef2).
2 parents 58b6ef2 + 3a5e84f commit 5314836

File tree

12 files changed

+633
-301
lines changed

12 files changed

+633
-301
lines changed

src/Symfony/Bundle/FrameworkBundle/Resources/translations/validators.en.xlf

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,18 @@
214214
<source>A PHP extension caused the upload to fail.</source>
215215
<target>A PHP extension caused the upload to fail.</target>
216216
</trans-unit>
217+
<trans-unit id="54">
218+
<source>This collection should contain {{ limit }} elements or more.</source>
219+
<target>This collection should contain {{ limit }} element or more.|This collection should contain {{ limit }} elements or more.</target>
220+
</trans-unit>
221+
<trans-unit id="55">
222+
<source>This collection should contain {{ limit }} elements or less.</source>
223+
<target>This collection should contain {{ limit }} element or less.|This collection should contain {{ limit }} elements or less.</target>
224+
</trans-unit>
225+
<trans-unit id="56">
226+
<source>This collection should contain exactly {{ limit }} elements.</source>
227+
<target>This collection should contain exactly {{ limit }} element.|This collection should contain exactly {{ limit }} elements.</target>
228+
</trans-unit>
217229
</body>
218230
</file>
219231
</xliff>

src/Symfony/Bundle/FrameworkBundle/Resources/translations/validators.fr.xlf

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,15 +76,15 @@
7676
</trans-unit>
7777
<trans-unit id="19">
7878
<source>This value is too long. It should have {{ limit }} characters or less.</source>
79-
<target>Cette chaine est trop longue. Elle doit avoir au maximum {{ limit }} caractères.</target>
79+
<target>Cette chaine est trop longue. Elle doit avoir au maximum {{ limit }} caractère.|Cette chaine est trop longue. Elle doit avoir au maximum {{ limit }} caractères.</target>
8080
</trans-unit>
8181
<trans-unit id="20">
8282
<source>This value should be {{ limit }} or more.</source>
8383
<target>Cette valeur doit être supérieure ou égale à {{ limit }}.</target>
8484
</trans-unit>
8585
<trans-unit id="21">
8686
<source>This value is too short. It should have {{ limit }} characters or more.</source>
87-
<target>Cette chaine est trop courte. Elle doit avoir au minimum {{ limit }} caractères.</target>
87+
<target>Cette chaine est trop courte. Elle doit avoir au minimum {{ limit }} caractère.|Cette chaine est trop courte. Elle doit avoir au minimum {{ limit }} caractères.</target>
8888
</trans-unit>
8989
<trans-unit id="22">
9090
<source>This value should not be blank.</source>
@@ -192,7 +192,7 @@
192192
</trans-unit>
193193
<trans-unit id="48">
194194
<source>This value should have exactly {{ limit }} characters.</source>
195-
<target>Cette chaine doit avoir exactement {{ limit }} caractères.</target>
195+
<target>Cette chaine doit avoir exactement {{ limit }} caractère.|Cette chaine doit avoir exactement {{ limit }} caractères.</target>
196196
</trans-unit>
197197
<trans-unit id="49">
198198
<source>The file was only partially uploaded.</source>
@@ -214,6 +214,18 @@
214214
<source>A PHP extension caused the upload to fail.</source>
215215
<target>Une extension PHP a empêché le transfert du fichier.</target>
216216
</trans-unit>
217+
<trans-unit id="54">
218+
<source>This collection should contain {{ limit }} elements or more.</source>
219+
<target>Cette collection doit contenir {{ limit }} élément ou plus.|Cette collection doit contenir {{ limit }} éléments ou plus.</target>
220+
</trans-unit>
221+
<trans-unit id="55">
222+
<source>This collection should contain {{ limit }} elements or less.</source>
223+
<target>Cette collection doit contenir {{ limit }} élément ou moins.|Cette collection doit contenir {{ limit }} éléments ou moins.</target>
224+
</trans-unit>
225+
<trans-unit id="56">
226+
<source>This collection should contain exactly {{ limit }} elements.</source>
227+
<target>Cette collection doit contenir exactement {{ limit }} élément.|Cette collection doit contenir exactement {{ limit }} éléments.</target>
228+
</trans-unit>
217229
</body>
218230
</file>
219231
</xliff>

src/Symfony/Component/Form/Extension/Validator/ValidatorTypeGuesser.php

Lines changed: 34 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -155,11 +155,19 @@ public function guessTypeForConstraint(Constraint $constraint)
155155
case 'Symfony\Component\Validator\Constraints\MaxLength':
156156
case 'Symfony\Component\Validator\Constraints\MinLength':
157157
case 'Symfony\Component\Validator\Constraints\Regex':
158-
case 'Symfony\Component\Validator\Constraints\SizeLength':
159158
return new TypeGuess('text', array(), Guess::LOW_CONFIDENCE);
160159

161-
case 'Symfony\Component\Validator\Constraints\Min':
162160
case 'Symfony\Component\Validator\Constraints\Size':
161+
switch ($constraint->type) {
162+
case 'string':
163+
return new TypeGuess('text', array(), Guess::LOW_CONFIDENCE);
164+
case 'collection':
165+
return new TypeGuess('collection', array(), Guess::MEDIUM_CONFIDENCE);
166+
}
167+
break;
168+
169+
case 'Symfony\Component\Validator\Constraints\Min':
170+
case 'Symfony\Component\Validator\Constraints\Range':
163171
case 'Symfony\Component\Validator\Constraints\Max':
164172
return new TypeGuess('number', array(), Guess::LOW_CONFIDENCE);
165173
}
@@ -194,8 +202,11 @@ public function guessMaxLengthForConstraint(Constraint $constraint)
194202
case 'Symfony\Component\Validator\Constraints\MaxLength':
195203
return new ValueGuess($constraint->limit, Guess::HIGH_CONFIDENCE);
196204

197-
case 'Symfony\Component\Validator\Constraints\SizeLength':
198-
return new ValueGuess($constraint->max, Guess::HIGH_CONFIDENCE);
205+
case 'Symfony\Component\Validator\Constraints\Size':
206+
if ('string' === $constraint->type && null !== $constraint->max) {
207+
return new ValueGuess($constraint->max, Guess::HIGH_CONFIDENCE);
208+
}
209+
break;
199210

200211
case 'Symfony\Component\Validator\Constraints\Type':
201212
if (in_array($constraint->type, array('double', 'float', 'numeric', 'real'))) {
@@ -206,7 +217,7 @@ public function guessMaxLengthForConstraint(Constraint $constraint)
206217
case 'Symfony\Component\Validator\Constraints\Max':
207218
return new ValueGuess(strlen((string) $constraint->limit), Guess::LOW_CONFIDENCE);
208219

209-
case 'Symfony\Component\Validator\Constraints\Size':
220+
case 'Symfony\Component\Validator\Constraints\Range':
210221
return new ValueGuess(strlen((string) $constraint->max), Guess::LOW_CONFIDENCE);
211222
}
212223
}
@@ -224,7 +235,23 @@ public function guessPatternForConstraint(Constraint $constraint)
224235
case 'Symfony\Component\Validator\Constraints\MinLength':
225236
return new ValueGuess(sprintf('.{%s,}', (string) $constraint->limit), Guess::LOW_CONFIDENCE);
226237

227-
case 'Symfony\Component\Validator\Constraints\SizeLength':
238+
case 'Symfony\Component\Validator\Constraints\Size':
239+
if ('string' !== $constraint->type) {
240+
return;
241+
}
242+
243+
if ($constraint->min === $constraint->max) {
244+
return new ValueGuess(sprintf('.{%s}', (string) $constraint->min), Guess::LOW_CONFIDENCE);
245+
}
246+
247+
if (null === $constraint->min) {
248+
return new ValueGuess(sprintf('.{0,%s}', (string) $constraint->max), Guess::LOW_CONFIDENCE);
249+
}
250+
251+
if (null === $constraint->max) {
252+
return new ValueGuess(sprintf('.{%s,}', (string) $constraint->min), Guess::LOW_CONFIDENCE);
253+
}
254+
228255
return new ValueGuess(sprintf('.{%s,%s}', (string) $constraint->min, (string) $constraint->max), Guess::LOW_CONFIDENCE);
229256

230257
case 'Symfony\Component\Validator\Constraints\Regex':
@@ -233,7 +260,7 @@ public function guessPatternForConstraint(Constraint $constraint)
233260
case 'Symfony\Component\Validator\Constraints\Min':
234261
return new ValueGuess(sprintf('.{%s,}', strlen((string) $constraint->limit)), Guess::LOW_CONFIDENCE);
235262

236-
case 'Symfony\Component\Validator\Constraints\Size':
263+
case 'Symfony\Component\Validator\Constraints\Range':
237264
return new ValueGuess(sprintf('.{%s,%s}', strlen((string) $constraint->min), strlen((string) $constraint->max)), Guess::LOW_CONFIDENCE);
238265

239266
case 'Symfony\Component\Validator\Constraints\Type':

src/Symfony/Component/Validator/CHANGELOG.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ CHANGELOG
55
-----
66

77
* added support for `ctype_*` assertions in `TypeValidator`
8-
* added a Size validator
9-
* added a SizeLength validator
8+
* added a Range validator for numeric values
9+
* added a Size validator for string & collections
1010
* improved the ImageValidator with min width, max width, min height, and max height constraints
1111
* added support for MIME with wildcard in FileValidator
1212
* changed Collection validator to add "missing" and "extra" errors to

src/Symfony/Component/Validator/Constraints/SizeLength.php renamed to src/Symfony/Component/Validator/Constraints/Range.php

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,13 @@
1818
*
1919
* @api
2020
*/
21-
class SizeLength extends Constraint
21+
class Range extends Constraint
2222
{
23-
public $minMessage = 'This value is too short. It should have {{ limit }} characters or more.';
24-
public $maxMessage = 'This value is too long. It should have {{ limit }} characters or less.';
25-
public $exactMessage = 'This value should have exactly {{ limit }} characters.';
23+
public $minMessage = 'This value should be {{ limit }} or more.';
24+
public $maxMessage = 'This value should be {{ limit }} or less.';
25+
public $invalidMessage = 'This value should be a valid number.';
2626
public $min;
2727
public $max;
28-
public $charset = 'UTF-8';
2928

3029
/**
3130
* {@inheritDoc}

src/Symfony/Component/Validator/Constraints/SizeLengthValidator.php renamed to src/Symfony/Component/Validator/Constraints/RangeValidator.php

Lines changed: 13 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -13,64 +13,52 @@
1313

1414
use Symfony\Component\Validator\Constraint;
1515
use Symfony\Component\Validator\ConstraintValidator;
16-
use Symfony\Component\Validator\Exception\UnexpectedTypeException;
1716

1817
/**
18+
* @author Bernhard Schussek <bschussek@gmail.com>
19+
*
1920
* @api
2021
*/
21-
class SizeLengthValidator extends ConstraintValidator
22+
class RangeValidator extends ConstraintValidator
2223
{
2324
/**
2425
* Checks if the passed value is valid.
2526
*
2627
* @param mixed $value The value that should be validated
2728
* @param Constraint $constraint The constraint for the validation
2829
*
30+
* @return Boolean Whether or not the value is valid
31+
*
2932
* @api
3033
*/
3134
public function validate($value, Constraint $constraint)
3235
{
33-
if (null === $value || '' === $value) {
36+
if (null === $value) {
3437
return;
3538
}
3639

37-
if (!is_scalar($value) && !(is_object($value) && method_exists($value, '__toString'))) {
38-
throw new UnexpectedTypeException($value, 'string');
39-
}
40-
41-
$value = (string) $value;
42-
43-
if (function_exists('grapheme_strlen') && 'UTF-8' === $constraint->charset) {
44-
$length = grapheme_strlen($value);
45-
} elseif (function_exists('mb_strlen')) {
46-
$length = mb_strlen($value, $constraint->charset);
47-
} else {
48-
$length = strlen($value);
49-
}
50-
51-
if ($constraint->min == $constraint->max && $length != $constraint->max) {
52-
$this->context->addViolation($constraint->exactMessage, array(
40+
if (!is_numeric($value)) {
41+
$this->context->addViolation($constraint->invalidMessage, array(
5342
'{{ value }}' => $value,
54-
'{{ limit }}' => $constraint->max,
55-
), null, (int) $constraint->max);
43+
));
5644

5745
return;
5846
}
5947

60-
if ($length > $constraint->max) {
48+
if ($value > $constraint->max) {
6149
$this->context->addViolation($constraint->maxMessage, array(
6250
'{{ value }}' => $value,
6351
'{{ limit }}' => $constraint->max,
64-
), null, (int) $constraint->max);
52+
));
6553

6654
return;
6755
}
6856

69-
if ($length < $constraint->min) {
57+
if ($value < $constraint->min) {
7058
$this->context->addViolation($constraint->minMessage, array(
7159
'{{ value }}' => $value,
7260
'{{ limit }}' => $constraint->min,
73-
), null, (int) $constraint->min);
61+
));
7462
}
7563
}
7664
}

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

Lines changed: 61 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,17 +20,70 @@
2020
*/
2121
class Size extends Constraint
2222
{
23-
public $minMessage = 'This value should be {{ limit }} or more.';
24-
public $maxMessage = 'This value should be {{ limit }} or less.';
25-
public $invalidMessage = 'This value should be a valid number.';
23+
const TYPE_STRING = 'string';
24+
const TYPE_COLLECTION = 'collection';
25+
26+
public $minMessage;
27+
public $maxMessage;
28+
public $exactMessage;
29+
public $type;
2630
public $min;
2731
public $max;
32+
public $charset = 'UTF-8';
33+
34+
private $stringMinMessage = 'This value is too short. It should have {{ limit }} characters or more.';
35+
private $stringMaxMessage = 'This value is too long. It should have {{ limit }} characters or less.';
36+
private $stringExactMessage = 'This value should have exactly {{ limit }} characters.';
37+
38+
private $collectionMinMessage = 'This collection should contain {{ limit }} elements or more.';
39+
private $collectionMaxMessage = 'This collection should contain {{ limit }} elements or less.';
40+
private $collectionExactMessage = 'This collection should contain exactly {{ limit }} elements.';
41+
42+
public function getMinMessage($type)
43+
{
44+
if (null !== $this->minMessage) {
45+
return $this->minMessage;
46+
}
47+
48+
switch ($type) {
49+
case static::TYPE_STRING:
50+
return $this->stringMinMessage;
51+
case static::TYPE_COLLECTION:
52+
return $this->collectionMinMessage;
53+
default:
54+
throw new \InvalidArgumentException('Invalid type specified.');
55+
}
56+
}
57+
58+
public function getMaxMessage($type)
59+
{
60+
if (null !== $this->maxMessage) {
61+
return $this->maxMessage;
62+
}
2863

29-
/**
30-
* {@inheritDoc}
31-
*/
32-
public function getRequiredOptions()
64+
switch ($type) {
65+
case static::TYPE_STRING:
66+
return $this->stringMaxMessage;
67+
case static::TYPE_COLLECTION:
68+
return $this->collectionMaxMessage;
69+
default:
70+
throw new \InvalidArgumentException('Invalid type specified.');
71+
}
72+
}
73+
74+
public function getExactMessage($type)
3375
{
34-
return array('min', 'max');
76+
if (null !== $this->exactMessage) {
77+
return $this->exactMessage;
78+
}
79+
80+
switch ($type) {
81+
case static::TYPE_STRING:
82+
return $this->stringExactMessage;
83+
case static::TYPE_COLLECTION:
84+
return $this->collectionExactMessage;
85+
default:
86+
throw new \InvalidArgumentException('Invalid type specified.');
87+
}
3588
}
3689
}

0 commit comments

Comments
 (0)
0