Cover of the book Symfony 5: The Fast Track

Symfony 5: The Fast Track is the best book to learn modern Symfony development, from zero to production. +300 pages showcasing Symfony with Docker, APIs, queues & async tasks, Webpack, SPAs, etc.

Compound

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.

New in version 5.1: The Compound constraint was introduced in Symfony 5.1.

Applies to class or property or method
Options
Class Symfony\Component\Validator\Constraints\Compound
Validator 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\Constraints\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:

  • Annotations
     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    // src/User/RegisterUser.php
    namespace App\User;
    
    use App\Validator\Constraints as AcmeAssert;
    
    class RegisterUser
    {
        /**
         * @AcmeAssert\PasswordRequirements()
         */
        public $password;
    }
    
  • YAML
    1
    2
    3
    4
    5
    # config/validator/validation.yaml
    App\User\RegisterUser:
        properties:
            password:
                - App\Validator\Constraints\PasswordRequirements: ~
    
  • XML
     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    <!-- 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>
    
  • PHP
     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    // 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

groups

type: array | string

It defines the validation group or groups this constraint belongs to. Read more about validation groups.

payload

type: mixed default: null

This option can be used to attach arbitrary domain-specific data to a constraint. The configured payload is not used by the Validator component, but its processing is completely up to you.

For example, you may want to use several error levels to present failed constraints differently in the front-end depending on the severity of the error.

This work, including the code samples, is licensed under a Creative Commons BY-SA 3.0 license.