Movatterモバイル変換


[0]ホーム

URL:


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

Sign up
Appearance settings

Commit9ccf365

Browse files
committed
feature#46599 Add "negate" option to Expression constraint (fmata)
This PR was merged into the 6.2 branch.Discussion----------Add "negate" option to Expression constraint| Q | A| ------------- | ---| Branch? | 6.2| Bug fix? | no| New feature? | yes| Deprecations? | no| Tickets || License | MIT| Doc PR |symfony/symfony-docs#16852Using `Expression` constraint is tedious because the written expression is the opposite of the error message. Adding `negate` option (naming ?) as `Regex::$match` allow us to write condition in phase with the wording.Commits-------4bc245e Add "pass" option to Expression constraint
2 parentsef33227 +4bc245e commit9ccf365

File tree

5 files changed

+30
-4
lines changed

5 files changed

+30
-4
lines changed

‎src/Symfony/Component/Validator/CHANGELOG.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ CHANGELOG
55
---
66

77
* Deprecate the "loose" e-mail validation mode, use "html5" instead
8+
* Add the`negate` option to the`Expression` constraint, to inverse the logic of the violation's creation
89

910
6.1
1011
---

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,14 +40,16 @@ class Expression extends Constraint
4040
public$message ='This value is not valid.';
4141
public$expression;
4242
public$values = [];
43+
publicbool$negate =true;
4344

4445
publicfunction__construct(
4546
string|ExpressionObject|array|null$expression,
4647
string$message =null,
4748
array$values =null,
4849
array$groups =null,
4950
mixed$payload =null,
50-
array$options = []
51+
array$options = [],
52+
bool$negate =null,
5153
) {
5254
if (!class_exists(ExpressionLanguage::class)) {
5355
thrownewLogicException(sprintf('The "symfony/expression-language" component is required to use the "%s" constraint.',__CLASS__));
@@ -63,6 +65,7 @@ public function __construct(
6365

6466
$this->message =$message ??$this->message;
6567
$this->values =$values ??$this->values;
68+
$this->negate =$negate ??$this->negate;
6669
}
6770

6871
/**

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public function validate(mixed $value, Constraint $constraint)
4242
$variables['value'] =$value;
4343
$variables['this'] =$this->context->getObject();
4444

45-
if (!$this->getExpressionLanguage()->evaluate($constraint->expression,$variables)) {
45+
if ($constraint->negatexor$this->getExpressionLanguage()->evaluate($constraint->expression,$variables)) {
4646
$this->context->buildViolation($constraint->message)
4747
->setParameter('{{ value }}',$this->formatValue($value,self::OBJECT_TO_STRING))
4848
->setCode(Expression::EXPRESSION_FAILED_ERROR)

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,17 +26,20 @@ public function testAttributes()
2626
[$aConstraint] =$metadata->properties['a']->getConstraints();
2727
self::assertSame('value == "1"',$aConstraint->expression);
2828
self::assertSame([],$aConstraint->values);
29+
self::assertTrue($aConstraint->negate);
2930

3031
[$bConstraint] =$metadata->properties['b']->getConstraints();
3132
self::assertSame('value == "1"',$bConstraint->expression);
3233
self::assertSame('myMessage',$bConstraint->message);
3334
self::assertSame(['Default','ExpressionDummy'],$bConstraint->groups);
35+
self::assertTrue($bConstraint->negate);
3436

3537
[$cConstraint] =$metadata->properties['c']->getConstraints();
3638
self::assertSame('value == someVariable',$cConstraint->expression);
3739
self::assertSame(['someVariable' =>42],$cConstraint->values);
3840
self::assertSame(['foo'],$cConstraint->groups);
3941
self::assertSame('some attached data',$cConstraint->payload);
42+
self::assertFalse($cConstraint->negate);
4043
}
4144
}
4245

@@ -45,9 +48,9 @@ class ExpressionDummy
4548
#[Expression('value == "1"')]
4649
private$a;
4750

48-
#[Expression(expression:'value == "1"', message:'myMessage')]
51+
#[Expression(expression:'value == "1"', message:'myMessage', negate:true)]
4952
private$b;
5053

51-
#[Expression(expression:'value == someVariable', values: ['someVariable' =>42], groups: ['foo'], payload:'some attached data')]
54+
#[Expression(expression:'value == someVariable', values: ['someVariable' =>42], groups: ['foo'], payload:'some attached data', negate:false)]
5255
private$c;
5356
}

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -285,4 +285,23 @@ public function testPassingCustomValues()
285285

286286
$this->assertNoViolation();
287287
}
288+
289+
publicfunctiontestViolationOnPass()
290+
{
291+
$constraint =newExpression([
292+
'expression' =>'value + custom != 2',
293+
'values' => [
294+
'custom' =>1,
295+
],
296+
'negate' =>false,
297+
]);
298+
299+
$this->validator->validate(2,$constraint);
300+
301+
$this->buildViolation('This value is not valid.')
302+
->atPath('property.path')
303+
->setParameter('{{ value }}',2)
304+
->setCode(Expression::EXPRESSION_FAILED_ERROR)
305+
->assertRaised();
306+
}
288307
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp