Skip to content

Fixed formatting issues #6011

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

Closed
Closed
Changes from 1 commit
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
Prev Previous commit
Improvements
  • Loading branch information
javiereguiluz committed Dec 15, 2015
commit c1846f280484340c6c3384c3b2bc899e7355cfe7
209 changes: 104 additions & 105 deletions reference/constraints/Callback.rst
Original file line number Diff line number Diff line change
Expand Up @@ -132,112 +132,111 @@ methods
This is an array of the methods that should be executed during the validation
process. Each method can be one of the following formats:

1) **String method name**
1) String method name
.....................

If the name of a method is a simple string (e.g. ``isAuthorValid``),
that method will be called on the same object that's being validated
and the ``ExecutionContextInterface`` will be the only argument (see
the above example).
If the name of a method is a simple string (e.g. ``isAuthorValid``), that method
will be called on the same object that's being validated and the
``ExecutionContextInterface`` will be the only argument (see the above example).

2) **Static array callback**
............................

Each method can also be specified as a standard array callback:

.. configuration-block::

.. code-block:: php-annotations

// src/AppBundle/Entity/Author.php
use Symfony\Component\Validator\Constraints as Assert;

/**
* @Assert\Callback(methods={
* { "AppBundle\MyStaticValidatorClass", "isAuthorValid" }
* })
*/
class Author
{
}

.. code-block:: yaml

# src/AppBundle/Resources/config/validation.yml
AppBundle\Entity\Author:
constraints:
- Callback:
methods:
- [AppBundle\MyStaticValidatorClass, isAuthorValid]

.. code-block:: xml

<!-- src/AppBundle/Resources/config/validation.xml -->
<?xml version="1.0" encoding="UTF-8" ?>
<constraint-mapping xmlns="http://symfony.com/schema/dic/constraint-mapping"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://symfony.com/schema/dic/constraint-mapping http://symfony.com/schema/dic/constraint-mapping/constraint-mapping-1.0.xsd">

<class name="AppBundle\Entity\Author">
<constraint name="Callback">
<option name="methods">
<value>
<value>AppBundle\MyStaticValidatorClass</value>
<value>isAuthorValid</value>
</value>
</option>
</constraint>
</class>
</constraint-mapping>

.. code-block:: php

// src/AppBundle/Entity/Author.php

use Symfony\Component\Validator\Mapping\ClassMetadata;
use Symfony\Component\Validator\Constraints\Callback;

class Author
{
public $name;

public static function loadValidatorMetadata(ClassMetadata $metadata)
{
$metadata->addConstraint(new Callback(array(
'methods' => array(
array(
'AppBundle\MyStaticValidatorClass',
'isAuthorValid',
),
),
)));
}
}

In this case, the static method ``isAuthorValid`` will be called on
the ``AppBundle\MyStaticValidatorClass`` class. It's passed both
the original object being validated (e.g. ``Author``) as well as the
``ExecutionContextInterface``::

namespace AppBundle;

use Symfony\Component\Validator\ExecutionContextInterface;
use AppBundle\Entity\Author;

class MyStaticValidatorClass
{
public static function isAuthorValid(
Author $author,
ExecutionContextInterface $context
) {
// ...
}
}

.. tip::

If you specify your ``Callback`` constraint via PHP, then you also
have the option to make your callback either a PHP closure or a
non-static callback. It is *not* currently possible, however, to
specify a :term:`service` as a constraint. To validate using a service,
you should :doc:`create a custom validation constraint
</cookbook/validation/custom_constraint>` and add that new constraint
to your class.
Each method can also be specified as a standard array callback:

.. configuration-block::

.. code-block:: php-annotations

// src/AppBundle/Entity/Author.php
use Symfony\Component\Validator\Constraints as Assert;

/**
* @Assert\Callback(methods={
* { "AppBundle\MyStaticValidatorClass", "isAuthorValid" }
* })
*/
class Author
{
}

.. code-block:: yaml

# src/AppBundle/Resources/config/validation.yml
AppBundle\Entity\Author:
constraints:
- Callback:
methods:
- [AppBundle\MyStaticValidatorClass, isAuthorValid]

.. code-block:: xml

<!-- src/AppBundle/Resources/config/validation.xml -->
<?xml version="1.0" encoding="UTF-8" ?>
<constraint-mapping xmlns="http://symfony.com/schema/dic/constraint-mapping"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://symfony.com/schema/dic/constraint-mapping http://symfony.com/schema/dic/constraint-mapping/constraint-mapping-1.0.xsd">

<class name="AppBundle\Entity\Author">
<constraint name="Callback">
<option name="methods">
<value>
<value>AppBundle\MyStaticValidatorClass</value>
<value>isAuthorValid</value>
</value>
</option>
</constraint>
</class>
</constraint-mapping>

.. code-block:: php

// src/AppBundle/Entity/Author.php

use Symfony\Component\Validator\Mapping\ClassMetadata;
use Symfony\Component\Validator\Constraints\Callback;

class Author
{
public $name;

public static function loadValidatorMetadata(ClassMetadata $metadata)
{
$metadata->addConstraint(new Callback(array(
'methods' => array(
array(
'AppBundle\MyStaticValidatorClass',
'isAuthorValid',
),
),
)));
}
}

In this case, the static method ``isAuthorValid`` will be called on the
``AppBundle\MyStaticValidatorClass`` class. It's passed both the original object
being validated (e.g. ``Author``) as well as the ``ExecutionContextInterface``::

namespace AppBundle;

use Symfony\Component\Validator\ExecutionContextInterface;
use AppBundle\Entity\Author;

class MyStaticValidatorClass
{
public static function isAuthorValid(
Author $author,
ExecutionContextInterface $context
) {
// ...
}
}

.. tip::

If you specify your ``Callback`` constraint via PHP, then you also have the
option to make your callback either a PHP closure or a non-static callback.
It is *not* currently possible, however, to specify a :term:`service` as a
constraint. To validate using a service, you should :doc:`create a custom
validation constraint </cookbook/validation/custom_constraint>` and add that
new constraint to your class.