Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 3a5e84f

Browse files
committed
[Validator] Add CollectionSize constraint
[Validator] Rename constraint Size to Range [Validator] Rename constraint CollectionSize to Size [Validator] Merge the SizeLength into the Size constraint [Validator] Update messages in Size constraint for consistancy [Validator] Add english and french translation for Size messages [Validator] Tweak expected types for exceptions in SizeValidator [Validator] Fix CS in SizeValidator [Validator] Update the ValidatorTypeGuesser [Validator] Tweak SizeValidator [Validator] Update CHANGELOG [Validator] Complete previous CHANGELOG updates [Form] Update validator type guesser [Validator] Pluralize collection size english messages [Validator] Pluralize Size french messages
1 parent 46ffbd5 commit 3a5e84f

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 <[email protected]>
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)