8000 bug #36905 [Validator] Catch expected ValueError (derrabus) · symfony/symfony@42692d6 · GitHub
[go: up one dir, main page]

Skip to content
8000

Commit 42692d6

Browse files
bug #36905 [Validator] Catch expected ValueError (derrabus)
This PR was merged into the 3.4 branch. Discussion ---------- [Validator] Catch expected ValueError | Q | A | ------------- | --- | Branch? | 3.4 | Bug fix? | yes | New feature? | no | Deprecations? | no | Tickets | #36872 | License | MIT | Doc PR | N/A `mb_check_encoding()` raises a `ValueError` on php 8 if an invalid encoding was passed. The validator that was patched here is expected to fail gracefully in that situation. This PR restores that behavior under php 8. Maybe we can reconsider this behavior for Symfony 5.2. It feels a bit odd to me because we swallow a potential misconfiguration of the validator. Commits ------- 8f3f67f [Validator] Catch expected ValueError.
2 parents 475a715 + 8f3f67f commit 42692d6

File tree

1 file changed

+10
-2
lines changed

1 file changed

+10
-2
lines changed

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

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,14 @@ public function validate($value, Constraint $constraint)
3939

4040
$stringValue = (string) $value;
4141

42-
if (!$invalidCharset = !@mb_check_encoding($stringValue, $constraint->charset)) {
43-
$length = mb_strlen($stringValue, $constraint->charset);
42+
try {
43+
$invalidCharset = !@mb_check_encoding($stringValue, $constraint->charset);
44+
} catch (\ValueError $e) {
45+
if (!str_starts_with($e->getMessage(), 'mb_check_encoding(): Argument #2 ($encoding) must be a valid encoding')) {
46+
throw $e;
47+
}
48+
49 8AF3 +
$invalidCharset = true;
4450
}
4551

4652
if ($invalidCharset) {
@@ -54,6 +60,8 @@ public function validate($value, Constraint $constraint)
5460
return;
5561
}
5662

63+
$length = mb_strlen($stringValue, $constraint->charset);
64+
5765
if (null !== $constraint->max && $length > $constraint->max) {
5866
$this->context->buildViolation($constraint->min == $constraint->max ? $constraint->exactMessage : $constraint->maxMessage)
5967
->setParameter('{{ value }}', $this->formatValue($stringValue))

0 commit comments

Comments
 (0)
0