[Validator] Catch expected ValueError.
This commit is contained in:
parent
e220e7cc01
commit
8f3f67f82a
@ -39,8 +39,14 @@ class LengthValidator extends ConstraintValidator
|
|||||||
|
|
||||||
$stringValue = (string) $value;
|
$stringValue = (string) $value;
|
||||||
|
|
||||||
if (!$invalidCharset = !@mb_check_encoding($stringValue, $constraint->charset)) {
|
try {
|
||||||
$length = mb_strlen($stringValue, $constraint->charset);
|
$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) {
|
if ($invalidCharset) {
|
||||||
@ -54,6 +60,8 @@ class LengthValidator extends ConstraintValidator
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$length = mb_strlen($stringValue, $constraint->charset);
|
||||||
|
|
||||||
if (null !== $constraint->max && $length > $constraint->max) {
|
if (null !== $constraint->max && $length > $constraint->max) {
|
||||||
$this->context->buildViolation($constraint->min == $constraint->max ? $constraint->exactMessage : $constraint->maxMessage)
|
$this->context->buildViolation($constraint->min == $constraint->max ? $constraint->exactMessage : $constraint->maxMessage)
|
||||||
->setParameter('{{ value }}', $this->formatValue($stringValue))
|
->setParameter('{{ value }}', $this->formatValue($stringValue))
|
||||||
|
Reference in New Issue
Block a user