Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit cfc492e

Browse files
minor #47428 [Form][Validator] Fix checking result of DateTime::getLastErrors (nicolas-grekas)
This PR was merged into the 4.4 branch. Discussion ---------- [Form][Validator] Fix checking result of DateTime::getLastErrors | Q | A | ------------- | --- | Branch? | 4.4 | Bug fix? | yes | New feature? | no | Deprecations? | no | Tickets | - | License | MIT | Doc PR | - Spotted by our CI on PHP 8.2 This returns an array on PHP < 8.2 and `false` on 8.2: ```php DateTime::createFromFormat('Y', '2022'); var_dump(DateTime::getLastErrors()); ``` /cc `@derickr` FYI as this might also be considered as a BC break on PHP 8.2 Commits ------- 39cd15b Fix checking result of DateTime::getLastErrors
2 parents 2a1da92 + 39cd15b commit cfc492e

File tree

2 files changed

+2
-2
lines changed

2 files changed

+2
-2
lines changed

src/Symfony/Component/Form/Extension/Core/DataTransformer/DateTimeToStringTransformer.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ public function reverseTransform($value)
121121
$outputTz = new \DateTimeZone($this->outputTimezone);
122122
$dateTime = \DateTime::createFromFormat($this->parseFormat, $value, $outputTz);
123123

124-
$lastErrors = \DateTime::getLastErrors();
124+
$lastErrors = \DateTime::getLastErrors() ?: ['error_count' => 0, 'warning_count' => 0];
125125

126126
if (0 < $lastErrors['warning_count'] || 0 < $lastErrors['error_count']) {
127127
throw new TransformationFailedException(implode(', ', array_merge(array_values($lastErrors['warnings']), array_values($lastErrors['errors']))));

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public function validate($value, Constraint $constraint)
4848

4949
\DateTime::createFromFormat($constraint->format, $value);
5050

51-
$errors = \DateTime::getLastErrors();
51+
$errors = \DateTime::getLastErrors() ?: ['error_count' => 0, 'warnings' => []];
5252

5353
if (0 < $errors['error_count']) {
5454
$this->context->buildViolation($constraint->message)

0 commit comments

Comments
 (0)