Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 8f3f67f

Browse filesBrowse files
committed
[Validator] Catch expected ValueError.
1 parent e220e7c commit 8f3f67f
Copy full SHA for 8f3f67f

File tree

1 file changed

+10
-2
lines changed
Filter options

1 file changed

+10
-2
lines changed

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Validator/Constraints/LengthValidator.php
+10-2Lines 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+
$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)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.