Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Validator] Catch expected ValueError #36905

Merged
merged 1 commit into from May 23, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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')) {
xabbuh marked this conversation as resolved.
Show resolved Hide resolved
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