From 8f3f67f82aa0f23e8c0c709fa8f446b5dd75e9b4 Mon Sep 17 00:00:00 2001 From: "Alexander M. Turek" Date: Fri, 22 May 2020 18:14:17 +0200 Subject: [PATCH] [Validator] Catch expected ValueError. --- .../Validator/Constraints/LengthValidator.php | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/src/Symfony/Component/Validator/Constraints/LengthValidator.php b/src/Symfony/Component/Validator/Constraints/LengthValidator.php index 01955ed03571..0c11dfca8466 100644 --- a/src/Symfony/Component/Validator/Constraints/LengthValidator.php +++ b/src/Symfony/Component/Validator/Constraints/LengthValidator.php @@ -39,8 +39,14 @@ public function validate($value, Constraint $constraint) $stringValue = (string) $value; - if (!$invalidCharset = !@mb_check_encoding($stringValue, $constraint->charset)) { - $length = mb_strlen($stringValue, $constraint->charset); + try { + $invalidCharset = !@mb_check_encoding($stringValue, $constraint->charset); + } catch (\ValueError $e) { + if (!str_starts_with($e->getMessage(), 'mb_check_encoding(): Argument #2 ($encoding) must be a valid encoding')) { + throw $e; + } + + $invalidCharset = true; } if ($invalidCharset) { @@ -54,6 +60,8 @@ public function validate($value, Constraint $constraint) return; } + $length = mb_strlen($stringValue, $constraint->charset); + if (null !== $constraint->max && $length > $constraint->max) { $this->context->buildViolation($constraint->min == $constraint->max ? $constraint->exactMessage : $constraint->maxMessage) ->setParameter('{{ value }}', $this->formatValue($stringValue))