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 52abcbe commit 11436cb
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 3 deletions.
3 changes: 2 additions & 1 deletion composer.json
Expand Up @@ -31,7 +31,8 @@
"symfony/polyfill-intl-icu": "~1.0",
"symfony/polyfill-mbstring": "~1.0",
"symfony/polyfill-php56": "~1.0",
"symfony/polyfill-php70": "~1.6"
"symfony/polyfill-php70": "~1.6",
"symfony/polyfill-php80": "~1.17"
},
"replace": {
"symfony/asset": "self.version",
Expand Down
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
1 change: 1 addition & 0 deletions src/Symfony/Component/Validator/composer.json
Expand Up @@ -19,6 +19,7 @@
"php": "^5.5.9|>=7.0.8",
"symfony/polyfill-ctype": "~1.8",
"symfony/polyfill-mbstring": "~1.0",
"symfony/polyfill-php80": "~1.17",
"symfony/translation": "~2.8|~3.0|~4.0"
},
"require-dev": {
Expand Down

0 comments on commit 11436cb

Please sign in to comment.