Thanks to visit codestin.com
Credit goes to github.com

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

* Allow single integer for the `versions` option of the `Uuid` constraint
* Allow single constraint to be passed to the `constraints` option of the `When` constraint

6.3
---
Expand Down
8 changes: 7 additions & 1 deletion src/Symfony/Component/Validator/Constraints/When.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,12 @@

use Symfony\Component\ExpressionLanguage\Expression;
use Symfony\Component\ExpressionLanguage\ExpressionLanguage;
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\Exception\LogicException;

/**
* @Annotation
*
* @Target({"CLASS", "PROPERTY", "METHOD", "ANNOTATION"})
*/
#[\Attribute(\Attribute::TARGET_CLASS | \Attribute::TARGET_PROPERTY | \Attribute::TARGET_METHOD | \Attribute::IS_REPEATABLE)]
Expand All @@ -26,7 +28,7 @@ class When extends Composite
public $constraints = [];
public $values = [];

public function __construct(string|Expression|array $expression, array $constraints = null, array $values = null, array $groups = null, $payload = null, array $options = [])
public function __construct(string|Expression|array $expression, array|Constraint $constraints = null, array $values = null, array $groups = null, $payload = null, array $options = [])
{
if (!class_exists(ExpressionLanguage::class)) {
throw new LogicException(sprintf('The "symfony/expression-language" component is required to use the "%s" constraint. Try running "composer require symfony/expression-language".', __CLASS__));
Expand All @@ -39,6 +41,10 @@ public function __construct(string|Expression|array $expression, array $constrai
$options['constraints'] = $constraints;
}

if (isset($options['constraints']) && !\is_array($options['constraints'])) {
$options['constraints'] = [$options['constraints']];
}

if (null !== $groups) {
$options['groups'] = $groups;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ class WhenTestWithAttributes
], groups: ['foo'])]
private $bar;

#[When(expression: 'true', constraints: new NotNull(), groups: ['foo'])]
private $qux;

#[When(expression: 'true', constraints: [
new NotNull(),
new NotBlank(),
Expand Down
27 changes: 27 additions & 0 deletions src/Symfony/Component/Validator/Tests/Constraints/WhenTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,17 @@ public function testAnnotations()
], $barConstraint->constraints);
self::assertSame(['foo'], $barConstraint->groups);

[$quxConstraint] = $metadata->properties['qux']->getConstraints();

self::assertInstanceOf(When::class, $quxConstraint);
self::assertSame('true', $quxConstraint->expression);
self::assertEquals([
new NotNull([
'groups' => ['foo'],
]),
], $quxConstraint->constraints);
self::assertSame(['foo'], $quxConstraint->groups);

[$bazConstraint] = $metadata->getters['baz']->getConstraints();

self::assertInstanceOf(When::class, $bazConstraint);
Expand Down Expand Up @@ -152,6 +163,17 @@ public function testAttributes()
], $barConstraint->constraints);
self::assertSame(['foo'], $barConstraint->groups);

[$quxConstraint] = $metadata->properties['qux']->getConstraints();

self::assertInstanceOf(When::class, $quxConstraint);
self::assertSame('true', $quxConstraint->expression);
self::assertEquals([
new NotNull([
'groups' => ['foo'],
]),
], $quxConstraint->constraints);
self::assertSame(['foo'], $quxConstraint->groups);

[$bazConstraint] = $metadata->getters['baz']->getConstraints();

self::assertInstanceOf(When::class, $bazConstraint);
Expand Down Expand Up @@ -183,6 +205,11 @@ class WhenTestWithAnnotations
*/
private $bar;

/**
* @When(expression="true", constraints=@NotNull, groups={"foo"})
*/
private $qux;

/**
* @When(expression="true", constraints={@NotNull, @NotBlank})
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,17 @@ public function testConstraintsAreExecuted()
]));
}

public function testConstraintIsExecuted()
{
$constraint = new NotNull();
$this->expectValidateValue(0, 'Foo', [$constraint]);

$this->validator->validate('Foo', new When([
'expression' => 'true',
'constraints' => $constraint,
]));
}

public function testConstraintsAreExecutedWithNull()
{
$constraints = [
Expand Down