Skip to content

Commit 2ec049d

Browse files
committed
Removing the alias stuff - not required after symfony/symfony#17074
1 parent 55c260d commit 2ec049d

File tree

1 file changed

+14
-17
lines changed

1 file changed

+14
-17
lines changed

validation/custom_constraint.rst

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -167,44 +167,41 @@ 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-
}
205-
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.
201+
.. note::
202+
In earlier versions of Symfony, the tag required an ``alias`` key (usually set
203+
to the class name). This is still allowed your constraint's ``validateBy``
204+
method can return this alias (instead of a class name).
208205

209206
Class Constraint Validator
210207
~~~~~~~~~~~~~~~~~~~~~~~~~~

0 commit comments

Comments
 (0)