Skip to content

[WCM][Cookbook][Validator] Documentation for Target Aware Constraints #6002

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
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
43 changes: 43 additions & 0 deletions cookbook/validation/custom_constraint.rst
Original file line number Diff line number Diff line change
Expand Up @@ -274,3 +274,46 @@ not to the property:
<class name="AppBundle\Entity\AcmeEntity">
<constraint name="AppBundle\Validator\Constraints\ContainsAlphanumeric" />
</class>

Target Aware Constraints
........................

Because of class inheritance, the validator may being called on an instance of a
subclass of the one you actually defined the constraint on.

In most cases, you will be using class constraints to check invariants
on the given instance only. Therefore, this shouldn't be an issue.

However, you may sometimes want to check invariants on the whole set of
instances of the class or refering to another mapping specification of the
class.

A good example of this, is the :doc:`/reference/constraints/UniqueEntity`
constraint which needs to access the Doctrine Repository of the class on which
the constraint was declared.

For these use cases, make your Constraint class implements the
:class:`Symfony\\Component\\Validator\\Constraints\\TargetAwareConstraintInterface`
interface and use the
:class:`Symfony\\Component\\Validator\\Constraints\\TargetAwareConstraintTrait`
trait::

// src/AppBundle/Validator/Constraints/RegisteredInstances.php
namespace AppBundle\Validator\Constraints;

use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\Constraints\TargetAwareConstraintInterface;
use Symfony\Component\Validator\Constraints\TargetAwareConstraintTrait;

// ...

class RegisteredInstances extends Constraint implements TargetAwareConstraintInterface
{
use TargetAwareConstraintTrait;

// ...
}

With these two, your constraint will be automatically fed the name of the class
in its target option. You can then easily access the name of the class to
produce whatever behavior you need.