Skip to content

Removing the alias stuff - not required after symfony/symfony#17074 #6746

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Aug 2, 2016
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 14 additions & 16 deletions validation/custom_constraint.rst
Original file line number Diff line number Diff line change
Expand Up @@ -167,44 +167,42 @@ Constraint Validators with Dependencies
If your constraint validator has dependencies, such as a database connection,
it will need to be configured as a service in the Dependency Injection
Container. This service must include the ``validator.constraint_validator``
tag and should include an ``alias`` attribute to be used in the validatedBy method of your validator class:
tag so that the validation system knows about it::

.. configuration-block::

.. code-block:: yaml

# app/config/services.yml
services:
validator.unique.your_validator_name:
class: Fully\Qualified\Validator\Class\Name
validator.contains_alphanumeric:
class: AppBundle\Validator\Constraints\ContainsAlphanumericValidator
tags:
- { name: validator.constraint_validator, alias: alias_name }
- { name: validator.constraint_validator }

.. code-block:: xml

<!-- app/config/services.xml -->
<service id="validator.unique.your_validator_name" class="Fully\Qualified\Validator\Class\Name">
<service id="validator.contains_alphanumeric" class="AppBundle\Validator\Constraints\ContainsAlphanumericValidator">
<argument type="service" id="doctrine.orm.default_entity_manager" />
<tag name="validator.constraint_validator" alias="alias_name" />
<tag name="validator.constraint_validator" />
</service>

.. code-block:: php

// app/config/services.php
$container
->register('validator.unique.your_validator_name', 'Fully\Qualified\Validator\Class\Name')
->addTag('validator.constraint_validator', array('alias' => 'alias_name'));
->register('validator.contains_alphanumeric', 'AppBundle\Validator\Constraints\ContainsAlphanumericValidator')
->addTag('validator.constraint_validator');

As mentioned above, Symfony will automatically look for a class named after
the constraint, with ``Validator`` appended. You can override this in your constraint class::
Now, when Symfony looks for the ``ContainsAlphanumericValidator`` validator, it will
load this service from the container.

public function validatedBy()
{
return 'Fully\Qualified\ConstraintValidator\Class\Name'; // or 'alias_name' if provided
}
.. note::

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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@weaverryan The last sentence reads weird. Was it meant to be this way?


Class Constraint Validator
~~~~~~~~~~~~~~~~~~~~~~~~~~
Expand Down