Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Validator] Validate time without seconds #48907

Merged
merged 1 commit into from
Aug 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions src/Symfony/Component/Validator/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ CHANGELOG
* Deprecate `ValidatorBuilder::setDoctrineAnnotationReader()`
* Deprecate `ValidatorBuilder::addDefaultDoctrineAnnotationReader()`
* Add `number`, `finite-number` and `finite-float` types to `Type` constraint
* Add the `withSeconds` option to the `Time` constraint that allows to pass time without seconds

6.3
---
Expand Down
5 changes: 4 additions & 1 deletion src/Symfony/Component/Validator/Constraints/Time.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,16 +35,19 @@ class Time extends Constraint
*/
protected static $errorNames = self::ERROR_NAMES;

public $withSeconds = true;
fabpot marked this conversation as resolved.
Show resolved Hide resolved
public $message = 'This value is not a valid time.';

public function __construct(
array $options = null,
string $message = null,
array $groups = null,
mixed $payload = null
mixed $payload = null,
bool $withSeconds = null,
) {
parent::__construct($options, $groups, $payload);

$this->withSeconds = $withSeconds ?? $this->withSeconds;
$this->message = $message ?? $this->message;
}
}
5 changes: 3 additions & 2 deletions src/Symfony/Component/Validator/Constraints/TimeValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
class TimeValidator extends ConstraintValidator
{
public const PATTERN = '/^(\d{2}):(\d{2}):(\d{2})$/';
public const PATTERN_WITHOUT_SECONDS = '/^(\d{2}):(\d{2})$/';

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

$value = (string) $value;

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

if (!self::checkTime($matches[1], $matches[2], $matches[3])) {
if (!self::checkTime($matches[1], $matches[2], $constraint->withSeconds ? $matches[3] : 0)) {
$this->context->buildViolation($constraint->message)
->setParameter('{{ value }}', $this->formatValue($value))
->setCode(Time::INVALID_TIME_ERROR)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,13 @@ public function testNullIsValid()
$this->assertNoViolation();
}

public function testDefaultWithSeconds()
{
$this->validator->validate('10:15:25', new Time());

$this->assertNoViolation();
}

public function testEmptyStringIsValid()
{
$this->validator->validate('', new Time());
Expand Down Expand Up @@ -62,6 +69,49 @@ public static function getValidTimes()
];
}

/**
* @dataProvider getValidTimesWithoutSeconds
*/
public function testValidTimesWithoutSeconds(string $time)
{
$this->validator->validate($time, new Time([
'withSeconds' => false,
]));

$this->assertNoViolation();
}

public static function getValidTimesWithoutSeconds()
{
return [
['01:02'],
['00:00'],
['23:59'],
];
}

/**
* @dataProvider getInvalidTimesWithoutSeconds
*/
public function testInvalidTimesWithoutSeconds(string $time)
{
$this->validator->validate($time, $constraint = new Time());

$this->buildViolation($constraint->message)
->setParameter('{{ value }}', '"'.$time.'"')
->setCode(Time::INVALID_FORMAT_ERROR)
->assertRaised();
}

public static function getInvalidTimesWithoutSeconds()
{
return [
['01:02'],
['00:00'],
['23:59'],
];
}

/**
* @dataProvider getInvalidTimes
*/
Expand Down