8000 [Validator][Date] Introduce format for date constraint by chadyred · Pull Request #58602 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Validator][Date] Introduce format for date constraint #58602

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

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/Symfony/Component/Validator/Constraints/Date.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,16 +31,18 @@ class Date extends Constraint
self::INVALID_DATE_ERROR => 'INVALID_DATE_ERROR',
];

public string $format = 'Y-m-d';
public string $message = 'This value is not a valid date.';

/**
* @param array<string,mixed>|null $options
* @param string[]|null $groups
*/
public function __construct(?array $options = null, ?string $message = null, ?array $groups = null, mixed $payload = null)
public function __construct(?array $options = null, ?string $message = null, ?array $groups = null, mixed $payload = null, ?string $format = null)
{
parent::__construct($options, $groups, $payload);

$this->message = $message ?? $this->message;
$this->format = $format ?? $this->format;
}
}
24 changes: 4 additions & 20 deletions src/Symfony/Component/Validator/Constraints/DateValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,6 @@ class DateValidator extends ConstraintValidator
{
public const PATTERN = '/^(?<year>\d{4})-(?<month>\d{2})-(?<day>\d{2})$/D';

/**
* Checks whether a date is valid.
*
* @internal
*/
public static function checkDate(int $year, int $month, int $day): bool
{
return checkdate($month, $day, $year);
}

public function validate(mixed $value, Constraint $constraint): void
{
if (!$constraint instanceof Date) {
Expand All @@ -49,20 +39,14 @@ public function validate(mixed $value, Constraint $constraint): void

$value = (string) $value;

if (!preg_match(static::PATTERN, $value, $matches)) {
$asDateTimeImmutable = \DateTimeImmutable::createFromFormat($constraint->format, $value);

if (!$asDateTimeImmutable) {
$this->context->buildViolation($constraint->message)
->setParameter('{{ value }}', $this->formatValue($value))
->setCode(Date::INVALID_FORMAT_ERROR)
->addViolation();

return;
}

if (!self::checkDate(
$matches['year'] ?? $matches[1],
$matches['month'] ?? $matches[2],
$matches['day'] ?? $matches[3]
)) {
} elseif ($asDateTimeImmutable->format($constraint->format) !== $value) {
$this->context->buildViolation($constraint->message)
->setParameter('{{ value }}', $this->formatValue($value))
->setCode(Date::INVALID_DATE_ERROR)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,4 +115,58 @@ public static function getInvalidDates()
['2010-02-29', Date::INVALID_DATE_ERROR],
];
}

/**
* @dataProvider getNotMatchingDatesAndFormats
*/
public function testInvalidFormats($date, $format, $code)
{
$constraint = new Date(message: 'myMessage', format: $format);

$this->validator->validate($date, $constraint);

$this->buildViolation('myMessage')
->setParameter('{{ value }}', '"'.$date.'"')
->setCode($code)
->assertRaised();
}

public static function getNotMatchingDatesAndFormats(): array
{
return [
['2010-03-24', 'm-m-Y', Date::INVALID_FORMAT_ERROR],
['2010-02-29', 'foo', Date::INVALID_FORMAT_ERROR],
['bob', 'Y-m-d', Date::INVALID_FORMAT_ERROR],
['01.05.2030', 'd.m/Y', Date::INVALID_FORMAT_ERROR],
['01.05.2030', 'd/m-Y', Date::INVALID_FORMAT_ERROR],
['01.05.2030', 'd/m.Y', Date::INVALID_FORMAT_ERROR],
];
}

/**
* @dataProvider getMatchingDatesAndFormats
*/
public function testValidFormats($date, $format)
{
$this->validator->validate($date, new Date(format: $format));

$this->assertNoViolation();
}

public static function getMatchingDatesAndFormats(): array
{
return [
['2010-01-01', 'Y-m-d'],
['12-12-1955', 'm-d-Y'],
['31-05-2030', 'd-m-Y'],
['2010/01/01', 'Y/m/d'],
['12/12/1955', 'm/d/Y'],
['31/05/2030', 'd/m/Y'],
['2010.01.01', 'Y.m.d'],
['12.11.2010', 'm.d.Y'],
['01.05.2030', 'd.m.Y'],
['01.05/2030', 'd.m/Y'],
['December 31, 1999', 'F d, Y'],
];
}
}
0