Skip to content

Commit 0a9b7f5

Browse files
[Validator] Add support of nested attributes for composite constraints
1 parent d46125a commit 0a9b7f5

File tree

11 files changed

+309
-14
lines changed

11 files changed

+309
-14
lines changed

src/Symfony/Component/Validator/Constraints/All.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,16 @@
1717
*
1818
* @author Bernhard Schussek <bschussek@gmail.com>
1919
*/
20+
#[\Attribute(\Attribute::TARGET_PROPERTY | \Attribute::TARGET_METHOD | \Attribute::IS_REPEATABLE)]
2021
class All extends Composite
2122
{
2223
public $constraints = [];
2324

25+
public function __construct($constraints = null, array $groups = null, $payload = null)
26+
{
27+
parent::__construct($constraints ?? [], $groups, $payload);
28+
}
29+
2430
public function getDefaultOption()
2531
{
2632
return 'constraints';

src/Symfony/Component/Validator/Constraints/AtLeastOneOf.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
*
1818
* @author Przemysław Bogusz <przemyslaw.bogusz@tubotax.pl>
1919
*/
20+
#[\Attribute(\Attribute::TARGET_PROPERTY | \Attribute::TARGET_METHOD | \Attribute::IS_REPEATABLE)]
2021
class AtLeastOneOf extends Composite
2122
{
2223
public const AT_LEAST_ONE_OF_ERROR = 'f27e6d6c-261a-4056-b391-6673a623531c';
@@ -30,6 +31,15 @@ class AtLeastOneOf extends Composite
3031
public $messageCollection = 'Each element of this collection should satisfy its own set of constraints.';
3132
public $includeInternalMessages = true;
3233

34+
public function __construct($constraints = null, array $groups = null, $payload = null, string $message = null, string $messageCollection = null, bool $includeInternalMessages = null)
35+
{
36+
parent::__construct($constraints ?? [], $groups, $payload);
37+
38+
$this->message = $message ?? $this->message;
39+
$this->messageCollection = $messageCollection ?? $this->messageCollection;
40+
$this->includeInternalMessages = $includeInternalMessages ?? $this->includeInternalMessages;
41+
}
42+
3343
public function getDefaultOption()
3444
{
3545
return 'constraints';

src/Symfony/Component/Validator/Constraints/Collection.php

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
*
2020
* @author Bernhard Schussek <bschussek@gmail.com>
2121
*/
22+
#[\Attribute(\Attribute::TARGET_PROPERTY | \Attribute::TARGET_METHOD | \Attribute::IS_REPEATABLE)]
2223
class Collection extends Composite
2324
{
2425
public const MISSING_FIELD_ERROR = '2fa2158c-2a7f-484b-98aa-975522539ff8';
@@ -38,15 +39,20 @@ class Collection extends Composite
3839
/**
3940
* {@inheritdoc}
4041
*/
41-
public function __construct($options = null)
42+
public function __construct($fields = null, array $groups = null, $payload = null, bool $allowExtraFields = null, bool $allowMissingFields = null, string $extraFieldsMessage = null, string $missingFieldsMessage = null)
4243
{
43-
// no known options set? $options is the fields array
44-
if (\is_array($options)
45-
&& !array_intersect(array_keys($options), ['groups', 'fields', 'allowExtraFields', 'allowMissingFields', 'extraFieldsMessage', 'missingFieldsMessage'])) {
46-
$options = ['fields' => $options];
44+
// no known options set? $fields is the fields array
45+
if (\is_array($fields)
46+
&& !array_intersect(array_keys($fields), ['groups', 'fields', 'allowExtraFields', 'allowMissingFields', 'extraFieldsMessage', 'missingFieldsMessage'])) {
47+
$fields = ['fields' => $fields];
4748
}
4849

49-
parent::__construct($options);
50+
parent::__construct($fields, $groups, $payload);
51+
52+
$this->allowExtraFields = $allowExtraFields ?? $this->allowExtraFields;
53+
$this->allowMissingFields = $allowMissingFields ?? $this->allowMissingFields;
54+
$this->extraFieldsMessage = $extraFieldsMessage ?? $this->extraFieldsMessage;
55+
$this->missingFieldsMessage = $missingFieldsMessage ?? $this->missingFieldsMessage;
5056
}
5157

5258
/**

src/Symfony/Component/Validator/Constraints/Composite.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,9 @@ abstract class Composite extends Constraint
5151
* cached. When constraints are loaded from the cache, no more group
5252
* checks need to be done.
5353
*/
54-
public function __construct($options = null)
54+
public function __construct($options = null, array $groups = null, $payload = null)
5555
{
56-
parent::__construct($options);
56+
parent::__construct($options, $groups, $payload);
5757

5858
$this->initializeNestedConstraints();
5959

src/Symfony/Component/Validator/Constraints/Sequentially.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,16 @@
2020
*
2121
* @author Maxime Steinhausser <maxime.steinhausser@gmail.com>
2222
*/
23+
#[\Attribute(\Attribute::TARGET_PROPERTY | \Attribute::TARGET_METHOD | \Attribute::IS_REPEATABLE)]
2324
class Sequentially extends Composite
2425
{
2526
public $constraints = [];
2627

28+
public function __construct($constraints = null, array $groups = null, $payload = null)
29+
{
30+
parent::__construct($constraints ?? [], $groups, $payload);
31+
}
32+
2733
public function getDefaultOption()
2834
{
2935
return 'constraints';

src/Symfony/Component/Validator/Tests/Fixtures/Annotation/Entity.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,10 @@ class Entity extends EntityParent implements EntityInterfaceB
3030
* @Assert\Collection(fields={
3131
* "foo" = {@Assert\NotNull, @Assert\Range(min=3)},
3232
* "bar" = @Assert\Range(min=5)
33-
* })
33+
* }, allowExtraFields=true)
3434
* @Assert\Choice(choices={"A", "B"}, message="Must be one of %choices%")
35+
* @Assert\AtLeastOneOf({@Assert\NotNull, @Assert\Range(min=3)}, message="foo", includeInternalMessages=false)
36+
* @Assert\Sequentially({@Assert\NotBlank, @Assert\Range(min=5)})
3537
*/
3638
public $firstName;
3739
/**

src/Symfony/Component/Validator/Tests/Fixtures/Attribute/Entity.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,10 @@ class Entity extends EntityParent implements EntityInterfaceB
3030
* @Assert\Collection(fields={
3131
* "foo" = {@Assert\NotNull, @Assert\Range(min=3)},
3232
* "bar" = @Assert\Range(min=5)
33-
* })
33+
* }, allowExtraFields=true)
3434
* @Assert\Choice(choices={"A", "B"}, message="Must be one of %choices%")
35+
* @Assert\AtLeastOneOf({@Assert\NotNull, @Assert\Range(min=3)}, message="foo", includeInternalMessages=false)
36+
* @Assert\Sequentially({@Assert\NotBlank, @Assert\Range(min=5)})
3537
*/
3638
#[
3739
Assert\NotNull,
Lines changed: 170 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,170 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Validator\Tests\Fixtures\NestedAttribute;
13+
14+
use Symfony\Component\Validator\Constraints as Assert;
15+
use Symfony\Component\Validator\Context\ExecutionContextInterface;
16+
use Symfony\Component\Validator\Tests\Fixtures\Attribute\EntityParent;
17+
use Symfony\Component\Validator\Tests\Fixtures\EntityInterfaceB;
18+
use Symfony\Component\Validator\Tests\Fixtures\CallbackClass;
19+
use Symfony\Component\Validator\Tests\Fixtures\ConstraintA;
20+
21+
#[
22+
ConstraintA,
23+
Assert\GroupSequence(['Foo', 'Entity']),
24+
Assert\Callback([CallbackClass::class, 'callback']),
25+
]
26+
class Entity extends EntityParent implements EntityInterfaceB
27+
{
28+
#[
29+
Assert\NotNull,
30+
Assert\Range(min: 3),
31+
Assert\All([
32+
new Assert\NotNull(),
33+
new Assert\Range(min: 3),
34+
]),
35+
Assert\All(
36+
constraints: [
37+
new Assert\NotNull(),
38+
new Assert\Range(min: 3),
39+
],
40+
),
41+
Assert\Collection(
42+
fields: [
43+
'foo' => [
44+
new Assert\NotNull(),
45+
new Assert\Range(min: 3),
46+
],
47+
'bar' => new Assert\Range(min: 5),
48+
],
49+
allowExtraFields: true
50+
),
51+
Assert\Choice(choices: ['A', 'B'], message: 'Must be one of %choices%'),
52+
Assert\AtLeastOneOf(
53+
constraints: [
54+
new Assert\NotNull(),
55+
new Assert\Range(min: 3),
56+
],
57+
message: 'foo',
58+
includeInternalMessages: false,
59+
),
60+
Assert\Sequentially([
61+
new Assert\NotBlank(),
62+
new Assert\Range(min: 5),
63+
]),
64+
]
65+
public $firstName;
66+
#[Assert\Valid]
67+
public $childA;
68+
#[Assert\Valid]
69+
public $childB;
70+
protected $lastName;
71+
public $reference;
72+
public $reference2;
73+
private $internal;
74+
public $data = 'Overridden data';
75+
public $initialized = false;
76+
77+
public function __construct($internal = null)
78+
{
79+
$this->internal = $internal;
80+
}
81+
82+
public function getFirstName()
83+
{
84+
return $this->firstName;
85+
}
86+
87+
public function getInternal()
88+
{
89+
return $this->internal.' from getter';
90+
}
91+
92+
public function setLastName($lastName)
93+
{
94+
$this->lastName = $lastName;
95+
}
96+
97+
#[Assert\NotNull]
98+
public function getLastName()
99+
{
100+
return $this->lastName;
101+
}
102+
103+
public function getValid()
104+
{
105+
}
106+
107+
#[Assert\IsTrue]
108+
public function isValid()
109+
{
110+
return 'valid';
111+
}
112+
113+
#[Assert\IsTrue]
114+
public function hasPermissions()
115+
{
116+
return 'permissions';
117+
}
118+
119+
public function getData()
120+
{
121+
return 'Overridden data';
122+
}
123+
124+
#[Assert\Callback(payload: 'foo')]
125+
public function validateMe(ExecutionContextInterface $context)
126+
{
127+
}
128+
129+
#[Assert\Callback]
130+
public static function validateMeStatic($object, ExecutionContextInterface $context)
131+
{
132+
}
133+
134+
/**
135+
* @return mixed
136+
*/
137+
public function getChildA()
138+
{
139+
return $this->childA;
140+
}
141+
142+
/**
143+
* @param mixed $childA
144+
*/
145+
public function setChildA($childA)
146+
{
147+
$this->childA = $childA;
148+
}
149+
150+
/**
151+
* @return mixed
152+
*/
153+
public function getChildB()
154+
{
155+
return $this->childB;
156+
}
157+
158+
/**
159+
* @param mixed $childB
160+
*/
161+
public function setChildB($childB)
162+
{
163+
$this->childB = $childB;
164+
}
165+
166+
public function getReference()
167+
{
168+
return $this->reference;
169+
}
170+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Validator\Tests\Fixtures\NestedAttribute;
13+
14+
use Symfony\Component\Validator\Constraints\NotNull;
15+
use Symfony\Component\Validator\Tests\Fixtures\EntityInterfaceA;
16+
17+
class EntityParent implements EntityInterfaceA
18+
{
19+
protected $firstName;
20+
private $internal;
21+
private $data = 'Data';
22+
private $child;
23+
24+
#[NotNull]
25+
protected $other;
26+
27+
public function getData()
28+
{
29+
return 'Data';
30+
}
31+
32+
public function getChild()
33+
{
34+
return $this->child;
35+
}
36+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Validator\Tests\Fixtures\NestedAttribute;
13+
14+
use Symfony\Component\Validator\Constraints as Assert;
15+
use Symfony\Component\Validator\GroupSequenceProviderInterface;
16+
17+
#[Assert\GroupSequenceProvider]
18+
class GroupSequenceProviderEntity implements GroupSequenceProviderInterface
19+
{
20+
public $firstName;
21+
public $lastName;
22+
23+
protected $sequence = [];
24+
25+
public function __construct($sequence)
26+
{
27+
$this->sequence = $sequence;
28+
}
29+
30+
public function getGroupSequence()
31+
{
32+
return $this->sequence;
33+
}
34+
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy