@@ -167,44 +167,41 @@ Constraint Validators with Dependencies
167
167
If your constraint validator has dependencies, such as a database connection,
168
168
it will need to be configured as a service in the Dependency Injection
169
169
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: :
171
171
172
172
.. configuration-block ::
173
173
174
174
.. code-block :: yaml
175
175
176
176
# app/config/services.yml
177
177
services :
178
- validator.unique.your_validator_name :
179
- class : Fully\Qualified\ Validator\Class\Name
178
+ validator.contains_alphanumeric :
179
+ class : AppBundle\ Validator\Constraints\ContainsAlphanumericValidator
180
180
tags :
181
- - { name: validator.constraint_validator, alias: alias_name }
181
+ - { name: validator.constraint_validator }
182
182
183
183
.. code-block :: xml
184
184
185
185
<!-- 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 " >
187
187
<argument type =" service" id =" doctrine.orm.default_entity_manager" />
188
- <tag name =" validator.constraint_validator" alias = " alias_name " />
188
+ <tag name =" validator.constraint_validator" />
189
189
</service >
190
190
191
191
.. code-block :: php
192
192
193
193
// app/config/services.php
194
194
$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');
197
197
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.
200
200
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).
208
205
209
206
Class Constraint Validator
210
207
~~~~~~~~~~~~~~~~~~~~~~~~~~
0 commit comments