From bb5706744c7d96f43e4561ef42d4422c7846e063 Mon Sep 17 00:00:00 2001 From: Samuel Chiriluta Date: Mon, 9 Jan 2017 14:39:49 +0200 Subject: [PATCH 1/2] Custom constraint with dependencies needs an alias It seems that Symfony 2.7 still needs an alias for the service declared for a custom constraints. Otherwise, it's not loaded as a service from the container, so the dependencies are not injected. --- validation/custom_constraint.rst | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/validation/custom_constraint.rst b/validation/custom_constraint.rst index 16ffb756a52..f98bdb0e4ab 100644 --- a/validation/custom_constraint.rst +++ b/validation/custom_constraint.rst @@ -167,7 +167,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 so that the validation system knows about it: +tag and an ``alias`` attribute: .. configuration-block:: @@ -178,14 +178,14 @@ tag so that the validation system knows about it: app.contains_alphanumeric_validator: class: AppBundle\Validator\Constraints\ContainsAlphanumericValidator tags: - - { name: validator.constraint_validator } + - { name: validator.constraint_validator, alias: alias_name } .. code-block:: xml - + .. code-block:: php @@ -195,16 +195,16 @@ tag so that the validation system knows about it: $container ->register('app.contains_alphanumeric_validator', ContainsAlphanumericValidator::class) - ->addTag('validator.constraint_validator'); + ->addTag('validator.constraint_validator', array('alias' => 'alias_name')); -Now, when Symfony looks for the ``ContainsAlphanumericValidator`` validator, it will -load this service from the container. +Your constraint class should now use this alias to reference the appropriate validator: -.. note:: + public function validatedBy() + { + return 'alias_name'; + } - In earlier versions of Symfony, the tag required an ``alias`` key (usually set - to the class name). This is still allowed your constraint's ``validateBy()`` - method can return this alias (instead of a class name). +As mentioned above, Symfony2 will automatically look for a class named after the constraint, with ``Validator`` appended. If your constraint validator is defined as a service, it’s important that you override the ``validatedBy()`` method to return the alias used when defining your service, otherwise Symfony2 won’t use the constraint validator service, and will instantiate the class instead, without any dependencies injected. Class Constraint Validator ~~~~~~~~~~~~~~~~~~~~~~~~~~ From 3f69d807186050f6028103d778ae1a7960624622 Mon Sep 17 00:00:00 2001 From: Samuel Chiriluta Date: Mon, 9 Jan 2017 14:56:17 +0200 Subject: [PATCH 2/2] Fixed unexpected indentation --- validation/custom_constraint.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/validation/custom_constraint.rst b/validation/custom_constraint.rst index f98bdb0e4ab..d00f69096e5 100644 --- a/validation/custom_constraint.rst +++ b/validation/custom_constraint.rst @@ -197,7 +197,7 @@ tag and an ``alias`` attribute: ->register('app.contains_alphanumeric_validator', ContainsAlphanumericValidator::class) ->addTag('validator.constraint_validator', array('alias' => 'alias_name')); -Your constraint class should now use this alias to reference the appropriate validator: +Your constraint class should now use this alias to reference the appropriate validator:: public function validatedBy() {