Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/Symfony/Component/Validator/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ CHANGELOG
---

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

6.1
---
Expand Down
5 changes: 4 additions & 1 deletion src/Symfony/Component/Validator/Constraints/Expression.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,16 @@ class Expression extends Constraint
public $message = 'This value is not valid.';
public $expression;
public $values = [];
public bool $negate = true;

public function __construct(
string|ExpressionObject|array|null $expression,
string $message = null,
array $values = null,
array $groups = null,
mixed $payload = null,
array $options = []
array $options = [],
bool $negate = null,
) {
if (!class_exists(ExpressionLanguage::class)) {
throw new LogicException(sprintf('The "symfony/expression-language" component is required to use the "%s" constraint.', __CLASS__));
Expand All @@ -63,6 +65,7 @@ public function __construct(

$this->message = $message ?? $this->message;
$this->values = $values ?? $this->values;
$this->negate = $negate ?? $this->negate;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public function validate(mixed $value, Constraint $constraint)
$variables['value'] = $value;
$variables['this'] = $this->context->getObject();

if (!$this->getExpressionLanguage()->evaluate($constraint->expression, $variables)) {
if ($constraint->negate xor $this->getExpressionLanguage()->evaluate($constraint->expression, $variables)) {
$this->context->buildViolation($constraint->message)
->setParameter('{{ value }}', $this->formatValue($value, self::OBJECT_TO_STRING))
->setCode(Expression::EXPRESSION_FAILED_ERROR)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,20 @@ public function testAttributes()
[$aConstraint] = $metadata->properties['a']->getConstraints();
self::assertSame('value == "1"', $aConstraint->expression);
self::assertSame([], $aConstraint->values);
self::assertTrue($aConstraint->negate);

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

[$cConstraint] = $metadata->properties['c']->getConstraints();
self::assertSame('value == someVariable', $cConstraint->expression);
self::assertSame(['someVariable' => 42], $cConstraint->values);
self::assertSame(['foo'], $cConstraint->groups);
self::assertSame('some attached data', $cConstraint->payload);
self::assertFalse($cConstraint->negate);
}
}

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

#[Expression(expression: 'value == "1"', message: 'myMessage')]
#[Expression(expression: 'value == "1"', message: 'myMessage', negate: true)]
private $b;

#[Expression(expression: 'value == someVariable', values: ['someVariable' => 42], groups: ['foo'], payload: 'some attached data')]
#[Expression(expression: 'value == someVariable', values: ['someVariable' => 42], groups: ['foo'], payload: 'some attached data', negate: false)]
private $c;
}
Original file line number Diff line number Diff line change
Expand Up @@ -285,4 +285,23 @@ public function testPassingCustomValues()

$this->assertNoViolation();
}

public function testViolationOnPass()
{
$constraint = new Expression([
'expression' => 'value + custom != 2',
'values' => [
'custom' => 1,
],
'negate' => false,
]);

$this->validator->validate(2, $constraint);

$this->buildViolation('This value is not valid.')
->atPath('property.path')
->setParameter('{{ value }}', 2)
->setCode(Expression::EXPRESSION_FAILED_ERROR)
->assertRaised();
}
}