8000 [Validator] Add invalid datetime message in Range validator · symfony/symfony@c777306 · GitHub
[go: up one dir, main page]

Skip to content

Commit c777306

Browse files
przemyslaw-boguszfabpot
authored andcommitted
[Validator] Add invalid datetime message in Range validator
1 parent b5587b2 commit c777306

File tree

3 files changed

+127
-11
lines changed

3 files changed

+127
-11
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ class Range extends Constraint
4141
public $minMessage = 'This value should be {{ limit }} or more.';
4242
public $maxMessage = 'This value should be {{ limit }} or less.';
4343
public $invalidMessage = 'This value should be a valid number.';
44+
public $invalidDateTimeMessage = 'This value should be a valid datetime.';
4445
public $min;
4546
public $minPropertyPath;
4647
public $max;

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

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -44,18 +44,25 @@ public function validate($value, Constraint $constraint)
4444
return;
4545
}
4646

47+
$min = $this->getLimit($constraint->minPropertyPath, $constraint->min, $constraint);
48+
$max = $this->getLimit($constraint->maxPropertyPath, $constraint->max, $constraint);
49+
4750
if (!is_numeric($value) && !$value instanceof \DateTimeInterface) {
48-
$this->context->buildViolation($constraint->invalidMessage)
49-
->setParameter('{{ value }}', $this->formatValue($value, self::PRETTY_DATE))
50-
->setCode(Range::INVALID_CHARACTERS_ERROR)
51-
->addViolation();
51+
if ($this->isParsableDatetimeString($min) && $this->isParsableDatetimeString($max)) {
52+
$this->context->buildViolation($constraint->invalidDateTimeMessage)
53+
->setParameter('{{ value }}', $this->formatValue($value, self::PRETTY_DATE))
54+
->setCode(Range::INVALID_CHARACTERS_ERROR)
55+
->addViolation();
56+
} else {
57+
$this->context->buildViolation($constraint->invalidMessage)
58+
->setParameter('{{ value }}', $this->formatValue($value, self::PRETTY_DATE))
59+
->setCode(Range::INVALID_CHARACTERS_ERROR)
60+
->addViolation();
61+
}
5262

5363
return;
5464
}
5565

56-
$min = $this->getLimit($constraint->minPropertyPath, $constraint->min, $constraint);
57-
$max = $this->getLimit($constraint->maxPropertyPath, $constraint->max, $constraint);
58-
5966
// Convert strings to DateTimes if comparing another DateTime
6067
// This allows to compare with any date/time value supported by
6168
// the DateTime constructor:
@@ -182,4 +189,23 @@ private function getPropertyAccessor(): PropertyAccessorInterface
182189

183190
return $this->propertyAccessor;
184191
}
192+
193+
private function isParsableDatetimeString($boundary): bool
194+
{
195+
if (null === $boundary) {
196+
return true;
197+
}
198+
199+
if (!\is_string($boundary)) {
200+
return false;
201+
}
202+
203+
try {
204+
new \DateTime($boundary);
205+
} catch (\Exception $e) {
206+
return false;
207+
}
208+
209+
return true;
210+
}
185211
}

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

Lines changed: 93 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -379,13 +379,102 @@ public function getInvalidValues()
379379

380380
public function testNonNumeric()
381381
{
382-
$this->validator->validate('abcd', new Range([
382+
$constraint = new Range([
383383
'min' => 10,
384384
'max' => 20,
385-
'invalidMessage' => 'myMessage',
386-
]));
385+
]);
387386

388-
$this->buildViolation('myMessage')
387+
$this->validator->validate('abcd', $constraint);
388+
389+
$ 10000 this->buildViolation($constraint->invalidMessage)
390+
->setParameter('{{ value }}', '"abcd"')
391+
->setCode(Range::INVALID_CHARACTERS_ERROR)
392+
->assertRaised();
393+
}
394+
395+
public function testNonNumericWithParsableDatetimeMinAndMaxNull()
396+
{
397+
$constraint = new Range([
398+
'min' => 'March 10, 2014',
399+
]);
400+
401+
$this->validator->validate('abcd', $constraint);
402+
403+
$this->buildViolation($constraint->invalidDateTimeMessage)
404+
->setParameter('{{ value }}', '"abcd"')
405+
->setCode(Range::INVALID_CHARACTERS_ERROR)
406+
->assertRaised();
407+
}
408+
409+
public function testNonNumericWithParsableDatetimeMaxAndMinNull()
410+
{
411+
$constraint = new Range([
412+
'max' => 'March 20, 2014',
413+
]);
414+
415+
$this->validator->validate('abcd', $constraint);
416+
417+
$this->buildViolation($constraint->invalidDateTimeMessage)
418+
->setParameter('{{ value }}', '"abcd"')
419+
->setCode(Range::INVALID_CHARACTERS_ERROR)
420+
->assertRaised();
421+
}
422+
423+
public function testNonNumericWithParsableDatetimeMinAndMax()
424+
{
425+
$constraint = new Range([
426+
'min' => 'March 10, 2014',
427+
'max' => 'March 20, 2014',
428+
]);
429+
430+
$this->validator->validate('abcd', $constraint);
431+
432+
$this->buildViolation($constraint->invalidDateTimeMessage)
433+
->setParameter('{{ value }}', '"abcd"')
434+
->setCode(Range::INVALID_CHARACTERS_ERROR)
435+
->assertRaised();
436+
}
437+
438+
public function testNonNumericWithNonParsableDatetimeMin()
439+
{
440+
$constraint = new Range([
441+
'min' => 'March 40, 2014',
442+
'max' => 'March 20, 2014',
443+
]);
444+
445+
$this->validator->validate('abcd', $constraint);
446+
447+
$this->buildViolation($constraint->invalidMessage)
448+
->setParameter('{{ value }}', '"abcd"')
449+
->setCode(Range::INVALID_CHARACTERS_ERROR)
450+
->assertRaised();
451+
}
452+
453+
public function testNonNumericWithNonParsableDatetimeMax()
454+
{
455+
$constraint = new Range([
456+
'min' => 'March 10, 2014',
457+
'max' => 'March 50, 2014',
458+
]);
459+
460+
$this->validator->validate('abcd', $constraint);
461+
462+
$this->buildViolation($constraint->invalidMessage)
463+
->setParameter('{{ value }}', '"abcd"')
464+
->setCode(Range::INVALID_CHARACTERS_ERROR)
465+
->assertRaised();
466+
}
467+
468+
public function testNonNumericWithNonParsableDatetimeMinAndMax()
469+
{
470+
$constraint = new Range([
471+
'min' => 'March 40, 2014',
472+
'max' => 'March 50, 2014',
473+
]);
474+
475+
$this->validator->validate('abcd', $constraint);
476+
477+
$this->buildViolation($constraint->invalidMessage)
389478
->setParameter('{{ value }}', '"abcd"')
390479
->setCode(Range::INVALID_CHARACTERS_ERROR)
391480
->assertRaised();

0 commit comments

Comments
< 2FA7 span class="prc-VisuallyHidden-VisuallyHidden-UNWQp"> (0)
0