Skip to content

Commit

Permalink
[Validator] Catch expected ValueError.
Browse files Browse the repository at this point in the history
  • Loading branch information
derrabus committed May 22, 2020
1 parent e220e7c commit 8f3f67f
Showing 1 changed file with 10 additions and 2 deletions.
12 changes: 10 additions & 2 deletions src/Symfony/Component/Validator/Constraints/LengthValidator.php
Expand Up @@ -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) {
Expand All @@ -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))
Expand Down

0 comments on commit 8f3f67f

Please sign in to comment.