8000 feature #48907 [Validator] Validate time without seconds (xepozz) · symfony/symfony@268a359 · GitHub
[go: up one dir, main page]

Skip to content

Commit 268a359

Browse files
feature #48907 [Validator] Validate time without seconds (xepozz)
This PR was merged into the 6.4 branch. Discussion ---------- [Validator] Validate time without seconds | Q | A | ------------- | --- | Branch? | 6.3 | Bug fix? | no | New feature? | yes | Deprecations? | no | License | MIT | Doc PR | symfony/symfony-docs#... <!-- required for new features --> Adds ability to validate time with the `Time` constraint without having the last part of the regexp that's responsible for seconds. I don't want to override the `Time` and create a new one with custom logic because I think that it maybe helpful for others. Tell me if I need to touch the docs Commits ------- 37617a8 [Validator] Add ability to validate time without seconds
2 parents 1300cc2 + 37617a8 commit 268a359

File tree

4 files changed

+58
-3
lines changed

4 files changed

+58
-3
lines changed

src/Symfony/Component/Validator/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ CHANGELOG
1111
* Deprecate `ValidatorBuilder::setDoctrineAnnotationReader()`
1212
* Deprecate `ValidatorBuilder::addDefaultDoctrineAnnotationReader()`
1313
* Add `number`, `finite-number` and `finite-float` types to `Type` constraint
14+
* Add the `withSeconds` option to the `Time` constraint that allows to pass time without seconds
1415

1516
6.3
1617
---

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,16 +35,19 @@ class Time extends Constraint
3535
*/
3636
protected static $errorNames = self::ERROR_NAMES;
3737

38+
public $withSeconds = true;
3839
public $message = 'This value is not a valid time.';
3940

4041
public function __construct(
4142
array $options = null,
4243
string $message = null,
4344
array $groups = null,
44-
mixed $payload = null
45+
mixed $payload = null,
46+
bool $withSeconds = null,
4547
) {
4648
parent::__construct($options, $groups, $payload);
4749

50+
$this->withSeconds = $withSeconds ?? $this->withSeconds;
4851
$this->message = $message ?? $this->message;
4952
}
5053
}

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
class TimeValidator extends ConstraintValidator
2323
{
2424
public const PATTERN = '/^(\d{2}):(\d{2}):(\d{2})$/';
25+
public const PATTERN_WITHOUT_SECONDS = '/^(\d{2}):(\d{2})$/';
2526

2627
/**
2728
* Checks whether a time is valid.
@@ -52,7 +53,7 @@ public function validate(mixed $value, Constraint $constraint)
5253

5354
$value = (string) $value;
5455

55-
if (!preg_match(static::PATTERN, $value, $matches)) {
56+
if (!preg_match($constraint->withSeconds ? static::PATTERN : static::PATTERN_WITHOUT_SECONDS, $value, $matches)) {
5657
$this->context->buildViolation($constraint->message)
5758
->setParameter('{{ value }}', $this->formatValue($value))
5859
->setCode(Time::INVALID_FORMAT_ERROR)
@@ -61,7 +62,7 @@ public function validate(mixed $value, Constraint $constraint)
6162
return;
6263
}
6364

64-
if (!self::checkTime($matches[1], $matches[2], $matches[3])) {
65+
if (!self::checkTime($matches[1], $matches[2], $constraint->withSeconds ? $matches[3] : 0)) {
6566
$this->context->buildViolation($constraint->message)
6667
->setParameter('{{ value }}', $this->formatValue($value))
6768
->setCode(Time::INVALID_TIME_ERROR)

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

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,13 @@ public function testNullIsValid()
3030
$this->assertNoViolation();
3131
}
3232

33+
public function testDefaultWithSeconds()
34+
{
35+
$this->validator->validate('10:15:25', new Time());
36+
37+
$this->assertNoViolation();
38+
}
39+
3340
public function testEmptyStringIsValid()
3441
{
3542
$this->validator->validate('', new Time());
@@ -62,6 +69,49 @@ public static function getValidTimes()
6269
];
6370
}
6471

72+
/**
73+
* @dataProvider getValidTimesWithoutSeconds
74+
*/
75+
public function testValidTimesWithoutSeconds(string $time)
76+
{
77+
$this->validator->validate($time, new Time([
78+
'withSeconds' => false,
79+
]));
80+
81+
$this->assertNoViolation();
82+
}
83+
84+
public static function getValidTimesWithoutSeconds()
85+
{
86+
return [
87+
['01:02'],
88+
['00:00'],
89+
['23:59'],
90+
];
91+
}
92+
93+
/**
94+
* @dataProvider getInvalidTimesWithoutSeconds
95+
*/
96+
public function testInvalidTimesWithoutSeconds(string $time)
97+
{
98+
$this->validator->validate($time, $constraint = new Time());
99+
100+
$this->buildViolation($constraint->message)
101+
->setParameter('{{ value }}', '"'.$time.'"')
102+
->setCode(Time::INVALID_FORMAT_ERROR)
103+
->assertRaised();
104+
}
105+
106+
public static function getInvalidTimesWithoutSeconds()
107+
{
108+
return [
109+
['01:02'],
110+
['00:00'],
111+
['23:59'],
112+
];
113+
}
114+
65115
/**
66116
* @dataProvider getInvalidTimes
67117
*/

0 commit comments

Comments
 (0)
0