Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
weaverryan merged 2 commits into2.7fromcustom_constraint_tweak
Aug 2, 2016
Merged
Changes fromall 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 deletionsvalidation/custom_constraint.rst
View file
Open in desktop
Original file line numberDiff line numberDiff 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``
tagand should include an ``alias`` attribute to be used inthevalidatedBy method of your validator class:
tagso thatthevalidation 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,Symfonywill automatically lookfora class named after
the constraint, with ``Validator`` appended. You can overridethisin your constraint class::
Now, whenSymfonylooksforthe ``ContainsAlphanumericValidator`` validator, it will
loadthisservice 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

[8]ページ先頭

©2009-2025 Movatter.jp