8000 [Validator] Add error's uid to `Count` and `Length` constraints with … · symfony/symfony@9fe5c62 · GitHub
[go: up one dir, main page]

Skip to content

Commit 9fe5c62

Browse files
[Validator] Add error's uid to Count and Length constraints with "exactly" option enabled
1 parent 5e291ce commit 9fe5c62

File tree

7 files changed

+28
-15
lines changed

7 files changed

+28
-15
lines changed

src/Symfony/Component/Validator/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ CHANGELOG
55
---
66

77
* Add support for `ConstraintViolationList::createFromMessage()`
8+
* Add error's uid to `Count` and `Length` constraints with "exactly" option enabled
89

910
5.3
1011
---

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,13 @@ class Count extends Constraint
2525
{
2626
public const TOO_FEW_ERROR = 'bef8e338-6ae5-4caf-b8e2-50e7b0579e69';
2727
public const TOO_MANY_ERROR = '756b1212-697c-468d-a9ad-50dd783bb169';
28+
public const NOT_EQUAL_COUNT_ERROR = '9fe5d43f-3784-4ece-a0e1-473fc02dadbc';
2829
public const NOT_DIVISIBLE_BY_ERROR = DivisibleBy::NOT_DIVISIBLE_BY;
2930

3031
protected static $errorNames = [
3132
self::TOO_FEW_ERROR => 'TOO_FEW_ERROR',
3233
self::TOO_MANY_ERROR => 'TOO_MANY_ERROR',
34+
self::NOT_EQUAL_COUNT_ERROR => 'NOT_EQUAL_COUNT_ERROR',
3335
self::NOT_DIVISIBLE_BY_ERROR => 'NOT_DIVISIBLE_BY_ERROR',
3436
];
3537

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

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,24 +41,28 @@ public function validate($value, Constraint $constraint)
4141
$count = \count($value);
4242

4343
if (null !== $constraint->max && $count > $constraint->max) {
44-
$this->context->buildViolation($constraint->min == $constraint->max ? $constraint->exactMessage : $constraint->maxMessage)
44+
$exactlyOptionEnabled = $constraint->min == $constraint->max;
45+
46+
$this->context->buildViolation($exactlyOptionEnabled ? $constraint->exactMessage : $constraint->maxMessage)
4547
->setParameter('{{ count }}', $count)
4648
->setParameter('{{ limit }}', $constraint->max)
4749
->setInvalidValue($value)
4850
->setPlural((int) $constraint->max)
49-
->setCode(Count::TOO_MANY_ERROR)
51+
->setCode($exactlyOptionEnabled ? Count::NOT_EQUAL_COUNT_ERROR : Count::TOO_MANY_ERROR)
5052
->addViolation();
5153

5254
return;
5355
}
5456

5557
if (null !== $constraint->min && $count < $constraint->min) {
56-
$this->context->buildViolation($constraint->min == $constraint->max ? $constraint->exactMessage : $constraint->minMessage)
58+
$exactlyOptionEnabled = $constraint->min == $constraint->max;
59+
60+
$this->context->buildViolation($exactlyOptionEnabled ? $constraint->exactMessage : $constraint->minMessage)
5761
->setParameter('{{ count }}', $count)
5862
->setParameter('{{ limit }}', $constraint->min)
5963
->setInvalidValue($value)
6064
->setPlural((int) $constraint->min)
61-
->setCode(Count::TOO_FEW_ERROR)
65+
->setCode($exactlyOptionEnabled ? Count::NOT_EQUAL_COUNT_ERROR : Count::TOO_FEW_ERROR)
6266
->addViolation();
6367

6468
return;

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,13 @@ class Length extends Constraint
2626
{
2727
public const TOO_SHORT_ERROR = '9ff3fdc4-b214-49db-8718-39c315e33d45';
2828
public const TOO_LONG_ERROR = 'd94b19cc-114f-4f44-9cc4-4138e80a87b9';
29+
public const NOT_EQUAL_LENGTH_ERROR = '4b6f5c76-22b4-409d-af16-fbe823ba9332';
2930
public const INVALID_CHARACTERS_ERROR = '35e6a710-aa2e-4719-b58e-24b35749b767';
3031

3132
protected static $errorNames = [
3233
self::TOO_SHORT_ERROR => 'TOO_SHORT_ERROR',
3334
self::TOO_LONG_ERROR => 'TOO_LONG_ERROR',
35+
self::NOT_EQUAL_LENGTH_ERROR => 'NOT_EQUAL_LENGTH_ERROR',
3436
self::INVALID_CHARACTERS_ERROR => 'INVALID_CHARACTERS_ERROR',
3537
];
3638

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

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,24 +68,28 @@ public function validate($value, Constraint $constraint)
6868
$length = mb_strlen($stringValue, $constraint->charset);
6969

7070
if (null !== $constraint->max && $length > $constraint->max) {
71-
$this->context->buildViolation($constraint->min == $constraint->max ? $constraint->exactMessage : $constraint->maxMessage)
71+
$exactlyOptionEnabled = $constraint->min == $constraint->max;
72+
73+
$this->context->buildViolation($exactlyOptionEnabled ? $constraint->exactMessage : $constraint->maxMessage)
7274
->setParameter('{{ value }}', $this->formatValue($stringValue))
7375
->setParameter('{{ limit }}', $constraint->max)
7476
->setInvalidValue($value)
7577
->setPlural((int) $constraint->max)
76-
->setCode(Length::TOO_LONG_ERROR)
78+
->setCode($exactlyOptionEnabled ? Length::NOT_EQUAL_LENGTH_ERROR : Length::TOO_LONG_ERROR)
7779
->addViolation();
7880

7981
return;
8082
}
8183

8284
if (null !== $constraint->min && $length < $constraint->min) {
83-
$this->context->buildViolation($constraint->min == $constraint->max ? $constraint->exactMessage : $constraint->minMessage)
85+
$exactlyOptionEnabled = $constraint->min == $constraint->max;
86+
87+
$this->context->buildViolation($exactlyOptionEnabled ? $constraint->exactMessage : $constraint->minMessage)
8488
->setParameter('{{ value }}', $this->formatValue($stringValue))
8589
->setParameter('{{ limit }}', $constraint->min)
8690
->setInvalidValue($value)
8791
->setPlural((int) $constraint->min)
88-
->setCode(Length::TOO_SHORT_ERROR)
92+
->setCode($exactlyOptionEnabled ? Length::NOT_EQUAL_LENGTH_ERROR : Length::TOO_SHORT_ERROR)
8993
->addViolation();
9094
}
9195
}

src/Symfony/Component/Validator/Tests/Constraints/CountValidatorTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,7 @@ public function testTooManyValuesExact($value)
236236
->setParameter('{{ limit }}', 4)
237237
->setInvalidValue($value)
238238
->setPlural(4)
239-
->setCode(Count::TOO_MANY_ERROR)
239+
->setCode(Count::NOT_EQUAL_COUNT_ERROR)
240240
->assertRaised();
241241
}
242242

@@ -255,7 +255,7 @@ public function testTooManyValuesExactNamed($value)
255255
->setParameter('{{ limit }}', 4)
256256
->setInvalidValue($value)
257257
->setPlural(4)
258-
->setCode(Count::TOO_MANY_ERROR)
258+
->setCode(Count::NOT_EQUAL_COUNT_ERROR)
259259
->assertRaised();
260260
}
261261

@@ -277,7 +277,7 @@ public function testTooFewValuesExact($value)
277277
->setParameter('{{ limit }}', 4)
278278
->setInvalidValue($value)
279279
->setPlural(4)
280-
->setCode(Count::TOO_FEW_ERROR)
280+
->setCode(Count::NOT_EQUAL_COUNT_ERROR)
281281
->assertRaised();
282282
}
283283

src/Symfony/Component/Validator/Tests/Constraints/LengthValidatorTest.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ public function testEmptyStringIsInvalid()
5252
->setParameter('{{ limit }}', $limit)
5353
->setInvalidValue('')
5454
->setPlural($limit)
55-
->setCode(Length::TOO_SHORT_ERROR)
55+
->setCode(Length::NOT_EQUAL_LENGTH_ERROR)
5656
->assertRaised();
5757
}
5858

@@ -264,7 +264,7 @@ public function testInvalidValuesExactLessThanFour($value)
264264
->setParameter('{{ limit }}', 4)
265265
->setInvalidValue($value)
266266
->setPlural(4)
267-
->setCode(Length::TOO_SHORT_ERROR)
267+
->setCode(Length::NOT_EQUAL_LENGTH_ERROR)
268268
->assertRaised();
269269
}
270270

@@ -283,7 +283,7 @@ public function testInvalidValuesExactLessThanFourNamed($value)
283283
->setParameter('{{ limit }}', 4)
284284
->setInvalidValue($value)
285285
->setPlural(4)
286-
->setCode(Length::TOO_SHORT_ERROR)
286+
->setCode(Length::NOT_EQUAL_LENGTH_ERROR)
287287
->assertRaised();
288288
}
289289

@@ -305,7 +305,7 @@ public function testInvalidValuesExactMoreThanFour($value)
305305
->setParameter('{{ limit }}', 4)
306306
->setInvalidValue($value)
307307
->setPlural(4)
308-
->setCode(Length::TOO_LONG_ERROR)
308+
->setCode(Length::NOT_EQUAL_LENGTH_ERROR)
309309
->assertRaised();
310310
}
311311

0 commit comments

Comments
 (0)
0