From 35c716e7ab497ff8336df82be947afef6d1a497e Mon Sep 17 00:00:00 2001 From: Axel Vankrunkelsven Date: Thu, 2 Jun 2016 17:49:57 +0200 Subject: [PATCH] Update custom_constraint.rst Clear confusion about defining a validator as a service and using it with a custom constraint --- cookbook/validation/custom_constraint.rst | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/cookbook/validation/custom_constraint.rst b/cookbook/validation/custom_constraint.rst index 0c42758dd93..e4903d7ff0d 100644 --- a/cookbook/validation/custom_constraint.rst +++ b/cookbook/validation/custom_constraint.rst @@ -158,7 +158,7 @@ Constraint Validators with Dependencies If your constraint validator has dependencies, such as a database connection, it will need to be configured as a service in the Dependency Injection Container. This service must include the ``validator.constraint_validator`` -tag and may include an ``alias`` attribute: +tag and should include an ``alias`` attribute to be used in the validatedBy method of your validator class: .. configuration-block:: @@ -194,6 +194,9 @@ the constraint, with ``Validator`` appended. You can override this in your const return 'Fully\Qualified\ConstraintValidator\Class\Name'; // or 'alias_name' if provided } +Make sure to use the 'alias_name' when you have configured your validator as a service. Otherwise your validator class +will be simply instantiated without your dependencies. + Class Constraint Validator ~~~~~~~~~~~~~~~~~~~~~~~~~~