Skip to content

Commit 8aa8b1e

Browse files
committed
feature #20283 [Validator] Add example for passing groups and payload to Compound (W0rma)
This PR was merged into the 7.2 branch. Discussion ---------- [Validator] Add example for passing groups and payload to Compound Closes #20152 Commits ------- 9ac77ee [Validator] Add example for passing groups and payload to the Compound constraint
2 parents 1b26f76 + 9ac77ee commit 8aa8b1e

File tree

1 file changed

+44
-0
lines changed

1 file changed

+44
-0
lines changed

reference/constraints/Compound.rst

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,50 @@ You can now use it anywhere you need it:
102102
}
103103
}
104104
105+
Validation groups and payload can be passed via constructor:
106+
107+
.. configuration-block::
108+
109+
.. code-block:: php-attributes
110+
111+
// src/Entity/User.php
112+
namespace App\Entity\User;
113+
114+
use App\Validator\Constraints as Assert;
115+
116+
class User
117+
{
118+
#[Assert\PasswordRequirements(
119+
groups: ['registration'],
120+
payload: ['severity' => 'error'],
121+
)]
122+
public string $plainPassword;
123+
}
124+
125+
.. code-block:: php
126+
127+
// src/Entity/User.php
128+
namespace App\Entity\User;
129+
130+
use App\Validator\Constraints as Assert;
131+
use Symfony\Component\Validator\Mapping\ClassMetadata;
132+
133+
class User
134+
{
135+
public static function loadValidatorMetadata(ClassMetadata $metadata): void
136+
{
137+
$metadata->addPropertyConstraint('plainPassword', new Assert\PasswordRequirements(
138+
groups: ['registration'],
139+
payload: ['severity' => 'error'],
140+
));
141+
}
142+
}
143+
144+
.. versionadded:: 7.2
145+
146+
Support for passing validation groups and the payload to the constructor
147+
of the ``Compound`` class was introduced in Symfony 7.2.
148+
105149
Options
106150
-------
107151

0 commit comments

Comments
 (0)