Skip to content

Commit cef06bb

Browse files
committed
feature #6746 Removing the alias stuff - not required after symfony/symfony#17074 (weaverryan)
This PR was merged into the 2.7 branch. Discussion ---------- Removing the alias stuff - not required after symfony/symfony#17074 Thanks to symfony/symfony#17074 (merged all the way back into 2.3), `alias` is not required anymore. So why mention it? As I understand it, validators work like the new form types: it's ok that `validatedBy returns a class name: the validation system will notice that you have a tagged service matching this class and will use that instead of creating it new. Commits ------- 31afb0e line break 2ec049d Removing the alias stuff - not required after symfony/symfony#17074
2 parents 55c260d + 31afb0e commit cef06bb

File tree

1 file changed

+14
-16
lines changed

1 file changed

+14
-16
lines changed

validation/custom_constraint.rst

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -167,44 +167,42 @@ Constraint Validators with Dependencies
167167
If your constraint validator has dependencies, such as a database connection,
168168
it will need to be configured as a service in the Dependency Injection
169169
Container. This service must include the ``validator.constraint_validator``
170-
tag and should include an ``alias`` attribute to be used in the validatedBy method of your validator class:
170+
tag so that the validation system knows about it::
171171

172172
.. configuration-block::
173173

174174
.. code-block:: yaml
175175
176176
# app/config/services.yml
177177
services:
178-
validator.unique.your_validator_name:
179-
class: Fully\Qualified\Validator\Class\Name
178+
validator.contains_alphanumeric:
179+
class: AppBundle\Validator\Constraints\ContainsAlphanumericValidator
180180
tags:
181-
- { name: validator.constraint_validator, alias: alias_name }
181+
- { name: validator.constraint_validator }
182182
183183
.. code-block:: xml
184184
185185
<!-- app/config/services.xml -->
186-
<service id="validator.unique.your_validator_name" class="Fully\Qualified\Validator\Class\Name">
186+
<service id="validator.contains_alphanumeric" class="AppBundle\Validator\Constraints\ContainsAlphanumericValidator">
187187
<argument type="service" id="doctrine.orm.default_entity_manager" />
188-
<tag name="validator.constraint_validator" alias="alias_name" />
188+
<tag name="validator.constraint_validator" />
189189
</service>
190190
191191
.. code-block:: php
192192
193193
// app/config/services.php
194194
$container
195-
->register('validator.unique.your_validator_name', 'Fully\Qualified\Validator\Class\Name')
196-
->addTag('validator.constraint_validator', array('alias' => 'alias_name'));
195+
->register('validator.contains_alphanumeric', 'AppBundle\Validator\Constraints\ContainsAlphanumericValidator')
196+
->addTag('validator.constraint_validator');
197197
198-
As mentioned above, Symfony will automatically look for a class named after
199-
the constraint, with ``Validator`` appended. You can override this in your constraint class::
198+
Now, when Symfony looks for the ``ContainsAlphanumericValidator`` validator, it will
199+
load this service from the container.
200200

201-
public function validatedBy()
202-
{
203-
return 'Fully\Qualified\ConstraintValidator\Class\Name'; // or 'alias_name' if provided
204-
}
201+
.. note::
205202

206-
Make sure to use the 'alias_name' when you have configured your validator as a service. Otherwise your validator class
207-
will be simply instantiated without your dependencies.
203+
In earlier versions of Symfony, the tag required an ``alias`` key (usually set
204+
to the class name). This is still allowed your constraint's ``validateBy``
205+
method can return this alias (instead of a class name).
208206

209207
Class Constraint Validator
210208
~~~~~~~~~~~~~~~~~~~~~~~~~~

0 commit comments

Comments
 (0)