diff --git a/reference/constraints.rst b/reference/constraints.rst index 317a836e396..7e7d588c58f 100644 --- a/reference/constraints.rst +++ b/reference/constraints.rst @@ -62,6 +62,7 @@ Validation Constraints Reference constraints/Isbn constraints/Issn + constraints/Compound constraints/Callback constraints/Expression constraints/All diff --git a/reference/constraints/Compound.rst b/reference/constraints/Compound.rst new file mode 100644 index 00000000000..8552058708c --- /dev/null +++ b/reference/constraints/Compound.rst @@ -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 ` or :ref:`property or method ` +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 + + + + + + + + + + + + + .. 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 diff --git a/reference/constraints/map.rst.inc b/reference/constraints/map.rst.inc index 337798c51da..85a40b766e9 100644 --- a/reference/constraints/map.rst.inc +++ b/reference/constraints/map.rst.inc @@ -83,6 +83,7 @@ Financial and other Number Constraints Other Constraints ~~~~~~~~~~~~~~~~~ +* :doc:`Compound ` * :doc:`Callback ` * :doc:`Expression ` * :doc:`All ` diff --git a/validation/custom_constraint.rst b/validation/custom_constraint.rst index b930f1084f4..68210299dc9 100644 --- a/validation/custom_constraint.rst +++ b/validation/custom_constraint.rst @@ -181,6 +181,16 @@ then your validator is already registered as a service and :doc:`tagged ` 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`. + +.. versionadded:: 5.1 + + The ``Compound`` constraint was introduced in Symfony 5.1. + Class Constraint Validator ~~~~~~~~~~~~~~~~~~~~~~~~~~