Skip to content

[Validator] Allow to define a reusable set of constraints #13195

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
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions reference/constraints.rst
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ Validation Constraints Reference
constraints/Isbn
constraints/Issn

constraints/Compound
constraints/Callback
constraints/Expression
constraints/All
Expand Down
111 changes: 111 additions & 0 deletions reference/constraints/Compound.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
Compound
========

To the contrary to the other constraints, this constraint cannot be used on its own.
Instead, it allows you to create your own set of reusable constraints, representing
rules to use consistently across your application, by extending the constraint.

.. versionadded:: 5.1

The ``Compound`` constraint was introduced in Symfony 5.1.

========== ===================================================================
Applies to :ref:`class <validation-class-target>` or :ref:`property or method <validation-property-target>`
Options - `groups`_
- `payload`_
Class :class:`Symfony\\Component\\Validator\\Constraints\\Compound`
Validator :class:`Symfony\\Component\\Validator\\Constraints\\CompoundValidator`
========== ===================================================================

Basic Usage
-----------

Suppose that you have different places where a user password must be validated,
you can create your own named set or requirements to be reused consistently everywhere::

// src/Validator/Constraints/PasswordRequirements.php
namespace App\Validator\Constraints;

use Symfony\Component\Validator\Compound;
use Symfony\Component\Validator\Constraints as Assert;

/**
* @Annotation
*/
class PasswordRequirements extends Compound
{
protected function getConstraints(array $options): array
{
return [
new Assert\NotBlank(),
new Assert\Type('string'),
new Assert\Length(['min' => 12]),
new Assert\NotCompromisedPassword(),
];
}
}

You can now use it anywhere you need it:

.. configuration-block::

.. code-block:: php-annotations

// src/User/RegisterUser.php
namespace App\User;

use App\Validator\Constraints as AcmeAssert;

class RegisterUser
{
/**
* @AcmeAssert\PasswordRequirements()
*/
public $password;
}

.. code-block:: yaml

# config/validator/validation.yaml
App\User\RegisterUser:
properties:
password:
- App\Validator\Constraints\PasswordRequirements: ~

.. code-block:: xml

<!-- config/validator/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 https://symfony.com/schema/dic/constraint-mapping/constraint-mapping-1.0.xsd">

<class name="App\User\RegisterUser">
<property name="password">
<constraint name="App\Validator\Constraints\PasswordRequirements"/>
</property>
</class>
</constraint-mapping>

.. code-block:: php

// src/User/RegisterUser.php
namespace App\User;

use App\Validator\Constraints as AcmeAssert;
use Symfony\Component\Validator\Mapping\ClassMetadata;

class RegisterUser
{
public static function loadValidatorMetadata(ClassMetadata $metadata)
{
$metadata->addPropertyConstraint('password', new AcmeAssert\PasswordRequirements());
}
}

Options
-------

.. include:: /reference/constraints/_groups-option.rst.inc

.. include:: /reference/constraints/_payload-option.rst.inc
1 change: 1 addition & 0 deletions reference/constraints/map.rst.inc
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ Financial and other Number Constraints
Other Constraints
~~~~~~~~~~~~~~~~~

* :doc:`Compound </reference/constraints/Compound>`
* :doc:`Callback </reference/constraints/Callback>`
* :doc:`Expression </reference/constraints/Expression>`
* :doc:`All </reference/constraints/All>`
Expand Down
10 changes: 10 additions & 0 deletions validation/custom_constraint.rst
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,16 @@ then your validator is already registered as a service and :doc:`tagged </servic
with the necessary ``validator.constraint_validator``. This means you can
:ref:`inject services or configuration <services-constructor-injection>` like any other service.

Create a Reusable Set of Constraints
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

In case you need to apply some common set of constraints in different places
consistently across your application, you can extend the :doc:`Compound constraint</reference/constraints/Compound>`.

.. versionadded:: 5.1

The ``Compound`` constraint was introduced in Symfony 5.1.

Class Constraint Validator
~~~~~~~~~~~~~~~~~~~~~~~~~~

Expand Down