Skip to content

Commit 873ede0

Browse files
author
Tristan Roussel
committed
Document constraint validator alias optional
1 parent 676f376 commit 873ede0

File tree

1 file changed

+4
-11
lines changed

1 file changed

+4
-11
lines changed

cookbook/validation/custom_constraint.rst

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ Constraint Validators with Dependencies
161161
If your constraint validator has dependencies, such as a database connection,
162162
it will need to be configured as a service in the Dependency Injection
163163
Container. This service must include the ``validator.constraint_validator``
164-
tag and an ``alias`` attribute:
164+
tag and may include an ``alias`` attribute:
165165

166166
.. configuration-block::
167167

@@ -189,21 +189,14 @@ tag and an ``alias`` attribute:
189189
->register('validator.unique.your_validator_name', 'Fully\Qualified\Validator\Class\Name')
190190
->addTag('validator.constraint_validator', array('alias' => 'alias_name'));
191191
192-
Your constraint class should now use this alias to reference the appropriate
193-
validator::
192+
As mentioned above, Symfony will automatically look for a class named after
193+
the constraint, with ``Validator`` appended. You can override this in your constraint class::
194194

195195
public function validatedBy()
196196
{
197-
return 'alias_name';
197+
return 'Fully\Qualified\ConstraintValidator\Class\Name'; // or 'alias_name' if provided
198198
}
199199

200-
As mentioned above, Symfony will automatically look for a class named after
201-
the constraint, with ``Validator`` appended. If your constraint validator
202-
is defined as a service, it's important that you override the
203-
``validatedBy()`` method to return the alias used when defining your service,
204-
otherwise Symfony won't use the constraint validator service, and will
205-
instantiate the class instead, without any dependencies injected.
206-
207200
Class Constraint Validator
208201
~~~~~~~~~~~~~~~~~~~~~~~~~~
209202

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy