8000 [Validator] [DateTime] Add `format` to error messages by saulius334 · Pull Request #58559 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,19 @@ class ChromePhpHandlerTest extends TestCase
{
public function testOnKernelResponseShouldNotTriggerDeprecation()
{
$this->expectNotToPerformAssertions();

$request = Request::create('/');
$request->headers->remove('User-Agent');

$response = new Response('foo');
$event = new ResponseEvent($this->createMock(HttpKernelInterface::class), $request, HttpKernelInterface::MAIN_REQUEST, $response);

$error = null;
set_error_handler(function ($type, $message) use (&$error) { $error = $message; }, \E_DEPRECATED);

$listener = new ChromePhpHandler();
$listener->onKernelResponse($event);
restore_error_handler();

$this->assertNull($error);
}
}
1 change: 1 addition & 0 deletions src/Symfony/Component/Validator/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ CHANGELOG
* Add the `Week` constraint
* Add `CompoundConstraintTestCase` to ease testing Compound Constraints
* Add context variable to `WhenValidator`
* Add `format` parameter to `DateTime` constraint violation message

7.1
---
Expand Down
7440
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ public function validate(mixed $value, Constraint $constraint): void
if (0 < $errors['error_count']) {
$this->context->buildViolation($constraint->message)
->setParameter('{{ value }}', $this->formatValue($value))
->setParameter('{{ format }}', $this->formatValue($constraint->format))
->setCode(DateTime::INVALID_FORMAT_ERROR)
->addViolation();

Expand All @@ -58,16 +59,19 @@ public function validate(mixed $value, Constraint $constraint): void
if ('The parsed date was invalid' === $warning) {
$this->context->buildViolation($constraint->message)
->setParameter('{{ value }}', $this->formatValue($value))
->setParameter('{{ format }}', $this->formatValue($constraint->format))
->setCode(DateTime::INVALID_DATE_ERROR)
->addViolation();
} elseif ('The parsed time was invalid' === $warning) {
$this->context->buildViolation($constraint->message)
->setParameter('{{ value }}', $this->formatValue($value))
->setParameter('{{ format }}', $this->formatValue($constraint->format))
->setCode(DateTime::INVALID_TIME_ERROR)
->addViolation();
} else {
$this->context->buildViolation($constraint->message)
->setParameter('{{ value }}', $this->formatValue($value))
->setParameter('{{ format }}', $this->formatValue($constraint->format))
->setCode(DateTime::INVALID_FORMAT_ERROR)
->addViolation();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ public function testDateTimeWithDefaultFormat()

$this->buildViolation('This value is not a valid datetime.')
->setParameter('{{ value }}', '"1995-03-24"')
->setParameter('{{ format }}', '"Y-m-d H:i:s"')
->setCode(DateTime::INVALID_FORMAT_ERROR)
->assertRaised();
}
Expand Down Expand Up @@ -96,6 +97,7 @@ public function testInvalidDateTimes($format, $dateTime, $code)

$this->buildViolation('myMessage')
->setParameter('{{ value }}', '"'.$dateTime.'"')
->setParameter('{{ format }}', '"'.$format.'"')
->setCode($code)
->assertRaised();
}
Expand Down Expand Up @@ -124,6 +126,7 @@ public function testInvalidDateTimeNamed()

$this->buildViolation('myMessage')
->setParameter('{{ value }}', '"2010-01-01 00:00:00"')
->setParameter('{{ format }}', '"Y-m-d"')
->setCode(DateTime::INVALID_FORMAT_ERROR)
->assertRaised();
}
Expand Down
Loading
0