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 10218cf

Browse filesBrowse files
committed
Add allowNullAndEmptyString option
1 parent 18aa137 commit 10218cf
Copy full SHA for 10218cf

File tree

Expand file treeCollapse file tree

4 files changed

+39
-4
lines changed
Filter options
Expand file treeCollapse file tree

4 files changed

+39
-4
lines changed

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Validator/Constraints/ExpressionLanguageSyntax.php
+7-1Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,20 @@ class ExpressionLanguageSyntax extends Constraint
3131
public $message = 'This value should be a valid expression.';
3232
public $service;
3333
public $allowedVariables;
34+
public $allowNullAndEmptyString = false;
3435

35-
public function __construct(array $options = null, string $message = null, string $service = null, array $allowedVariables = null, array $groups = null, $payload = null)
36+
public function __construct(array $options = null, string $message = null, string $service = null, array $allowedVariables = null, bool $allowNullAndEmptyString = null, array $groups = null, $payload = null)
3637
{
3738
parent::__construct($options, $groups, $payload);
3839

3940
$this->message = $message ?? $this->message;
4041
$this->service = $service ?? $this->service;
4142
$this->allowedVariables = $allowedVariables ?? $this->allowedVariables;
43+
$this->allowNullAndEmptyString = $allowNullAndEmptyString ?? $this->allowNullAndEmptyString;
44+
45+
if (isset($options['allowNullAndEmptyString'])) {
46+
trigger_deprecation('symfony/validator', '5.4', sprintf('The "allowNullAndEmptyString" option of the "%s" constraint is deprecated.', self::class));
47+
}
4248
}
4349

4450
/**

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Validator/Constraints/ExpressionLanguageSyntaxValidator.php
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public function validate($expression, Constraint $constraint): void
3939
throw new UnexpectedTypeException($constraint, ExpressionLanguageSyntax::class);
4040
}
4141

42-
if (null === $expression || '' === $expression) {
42+
if (true === $constraint->allowNullAndEmptyString && (null === $expression || '' === $expression)) {
4343
return;
4444
}
4545

‎src/Symfony/Component/Validator/Tests/Constraints/ExpressionLanguageSyntaxTest.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Validator/Tests/Constraints/ExpressionLanguageSyntaxTest.php
+7Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ public function testAttributes()
6161
[$aConstraint] = $metadata->properties['a']->getConstraints();
6262
self::assertNull($aConstraint->service);
6363
self::assertNull($aConstraint->allowedVariables);
64+
self::assertFalse($aConstraint->allowNullAndEmptyString);
6465

6566
[$bConstraint] = $metadata->properties['b']->getConstraints();
6667
self::assertSame('my_service', $bConstraint->service);
@@ -70,6 +71,9 @@ public function testAttributes()
7071
[$cConstraint] = $metadata->properties['c']->getConstraints();
7172
self::assertSame(['foo', 'bar'], $cConstraint->allowedVariables);
7273
self::assertSame(['my_group'], $cConstraint->groups);
74+
75+
[$dConstraint] = $metadata->properties['d']->getConstraints();
76+
self::assertTrue($dConstraint->allowNullAndEmptyString);
7377
}
7478
}
7579

@@ -83,4 +87,7 @@ class ExpressionLanguageSyntaxDummy
8387

8488
#[ExpressionLanguageSyntax(allowedVariables: ['foo', 'bar'], groups: ['my_group'])]
8589
private $c;
90+
91+
#[ExpressionLanguageSyntax(allowNullAndEmptyString: true)]
92+
private $d;
8693
}

‎src/Symfony/Component/Validator/Tests/Constraints/ExpressionLanguageSyntaxValidatorTest.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Validator/Tests/Constraints/ExpressionLanguageSyntaxValidatorTest.php
+24-2Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,15 +68,37 @@ public function testExpressionIsNotValid()
6868

6969
public function testNullIsValid()
7070
{
71-
$this->validator->validate(null, new ExpressionLanguageSyntax());
71+
$this->validator->validate(null, new ExpressionLanguageSyntax([
72+
'allowNullAndEmptyString' => true,
73+
]));
7274

7375
$this->assertNoViolation();
7476
}
7577

78+
public function testNullWithoutAllowOptionIsNotValid()
79+
{
80+
$this->expectExceptionMessage('Expected argument of type "string", "null" given');
81+
82+
$this->validator->validate(null, new ExpressionLanguageSyntax());
83+
}
84+
7685
public function testEmptyStringIsValid()
7786
{
78-
$this->validator->validate('', new ExpressionLanguageSyntax());
87+
$this->validator->validate('', new ExpressionLanguageSyntax([
88+
'allowNullAndEmptyString' => true,
89+
]));
7990

8091
$this->assertNoViolation();
8192
}
93+
94+
public function testEmptyStringWithoutAllowOptionIsNotValid()
95+
{
96+
$this->validator->validate('', new ExpressionLanguageSyntax());
97+
98+
$this->buildViolation('This value should be a valid expression.')
99+
->setParameter('{{ syntax_error }}', '"Unexpected token "end of expression" of value "" around position 1."')
100+
->setInvalidValue('')
101+
->setCode(ExpressionLanguageSyntax::EXPRESSION_LANGUAGE_SYNTAX_ERROR)
102+
->assertRaised();
103+
}
82104
}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.