8000 Merge branch '5.1' · symfony/validator@677ed3f · GitHub
[go: up one dir, main page]

Skip to content

Commit 677ed3f

Browse files
Merge branch '5.1'
* 5.1: (33 commits) [Cache] $lifetime cannot be null [Serializer] minor cleanup fix merge Run PHP 8 as 7.4.99 Remove calls to deprecated ReflectionParameter::getClass(). [VarDumper] fix PHP 8 support Removed "services" prototype node from "custom_authenticator" Add php 8 to travis. [Cache] Accessing undefined constants raises an Error in php8 [Cache] allow DBAL v3 Skip Doctrine DBAL on php 8 until we have a compatible version. [DomCrawler] Catch expected ValueError. Made method signatures compatible with their corresponding traits. [ErrorHandler] Apply php8 fixes from Debug component. [DomCrawler] Catch expected ValueError. [Validator] Catch expected ValueError. [VarDumper] ReflectionFunction::isDisabled() is deprecated. [BrowserKit] Raw body with custom Content-Type header Revert symfony/symfony#34986 Make ExpressionLanguageSyntax validator usable with annotation ...
2 parents dd717c3 + d49f884 commit 677ed3f

File tree

3 files changed

+16
-4
lines changed

3 files changed

+16
-4
lines changed

Constraints/ExpressionLanguageSyntax.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,6 @@ class ExpressionLanguageSyntax extends Constraint
3737
*/
3838
public function validatedBy()
3939
{
40-
return $this->service;
40+
return $this->service ?? static::class.'Validator';
4141
}
4242
}

Constraints/ExpressionLanguageSyntaxValidator.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ class ExpressionLanguageSyntaxValidator extends ConstraintValidator
2424
{
2525
private $expressionLanguage;
2626

27-
public function __construct(ExpressionLanguage $expressionLanguage)
27+
public function __construct(ExpressionLanguage $expressionLanguage = null)
2828
{
2929
$this->expressionLanguage = $expressionLanguage;
3030
}
@@ -42,6 +42,10 @@ public function validate($expression, Constraint $constraint): void
4242
throw new UnexpectedTypeException($expression, 'string');
4343
}
4444

45+
if (null === $this->expressionLanguage) {
46+
$this->expressionLanguage = new ExpressionLanguage();
47+
}
48+
4549
try {
4650
$this->expressionLanguage->lint($expression, ($constraint->validateNames ? ($constraint->names ?? []) : null));
4751
} catch (SyntaxError $exception) {

Constraints/LengthValidator.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,14 @@ public function validate($value, Constraint $constraint)
4444
$stringValue = ($constraint->normalizer)($stringValue);
4545
}
4646

47-
if (!$invalidCharset = !@mb_check_encoding($stringValue, $constraint->charset)) {
48-
$length = mb_strlen($stringValue, $constraint->charset);
47+
try {
48+
$invalidCharset = !@mb_check_encoding($stringValue, $constraint->charset);
49+
} catch (\ValueError $e) {
50+
if (!str_starts_with($e->getMessage(), 'mb_check_encoding(): Argument #2 ($encoding) must be a valid encoding')) {
51+
throw $e;
52+
}
53+
54+
$invalidCharset = true;
4955
}
5056

5157
if ($invalidCharset) {
@@ -59,6 +65,8 @@ public function validate($value, Constraint $constraint)
5965
return;
6066
}
6167

68+
$length = mb_strlen($stringValue, $constraint->charset);
69+
6270
if (null !== $constraint->max && $length > $constraint->max) {
6371
$this->context->buildViolation($constraint->min == $constraint->max ? $constraint->exactMessage : $constraint->maxMessage)
6472
->setParameter('{{ value }}', $this->formatValue($stringValue))

0 commit comments

Comments
 (0)
0