Skip to content

Commit 4f119ad

Browse files
committed
minor #9837 Added a missing validator example (javiereguiluz)
This PR was merged into the 2.8 branch. Discussion ---------- Added a missing validator example This fixes #6738. @xabbuh is this what you had in mind when you asked for an example in this section? Thanks! Commits ------- fd75230 Added a missing validator example
2 parents 1376c28 + fd75230 commit 4f119ad

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

components/validator/metadata.rst

+25
Original file line numberDiff line numberDiff line change
@@ -70,3 +70,28 @@ Classes
7070
Some constraints allow to validate the entire object. For example, the
7171
:doc:`Callback </reference/constraints/Callback>` constraint is a generic
7272
constraint that's applied to the class itself.
73+
74+
Suppose that the class defines a ``validate()`` method to hold its custom
75+
validation logic::
76+
77+
// ...
78+
use Symfony\Component\Validator\Context\ExecutionContextInterface;
79+
80+
public function validate(ExecutionContextInterface $context)
81+
{
82+
// ...
83+
}
84+
85+
Then, add the Validator component configuration to the class::
86+
87+
// ...
88+
use Symfony\Component\Validator\Mapping\ClassMetadata;
89+
use Symfony\Component\Validator\Constraints as Assert;
90+
91+
class Author
92+
{
93+
public static function loadValidatorMetadata(ClassMetadata $metadata)
94+
{
95+
$metadata->addConstraint(new Assert\Callback('validate'));
96+
}
97+
}

0 commit comments

Comments
 (0)