Skip to content

Commit 80594da

Browse files
committed
bug symfony#6634 Update custom_constraint.rst (axelvnk)
This PR was submitted for the 3.1 branch but it was merged into the 2.7 branch instead (closes symfony#6634). Discussion ---------- Update custom_constraint.rst Clear confusion about defining a validator as a service and using it with a custom constraint Commits ------- 6ae9022 Update custom_constraint.rst
2 parents dfa9f33 + 6ae9022 commit 80594da

File tree

1 file changed

+4
-1
lines changed

1 file changed

+4
-1
lines changed

cookbook/validation/custom_constraint.rst

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ 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 may include an ``alias`` attribute:
170+
tag and should include an ``alias`` attribute to be used in the validatedBy method of your validator class:
171171

172172
.. configuration-block::
173173

@@ -203,6 +203,9 @@ the constraint, with ``Validator`` appended. You can override this in your const
203203
return 'Fully\Qualified\ConstraintValidator\Class\Name'; // or 'alias_name' if provided
204204
}
205205

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.
208+
206209
Class Constraint Validator
207210
~~~~~~~~~~~~~~~~~~~~~~~~~~
208211

0 commit comments

Comments
 (0)