Skip to content

[Validator] Add PHP Attributes example to Compound #14977

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 1 commit into from
Feb 16, 2021
Merged
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
90 changes: 71 additions & 19 deletions reference/constraints/Compound.rst
Original file line number Diff line number Diff line change
Expand Up @@ -23,27 +23,66 @@ 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
.. configuration-block::

.. code-block:: php-annotations

// 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
{
return [
new Assert\NotBlank(),
new Assert\Type('string'),
new Assert\Length(['min' => 12]),
new Assert\NotCompromisedPassword(),
];
protected function getConstraints(array $options): array
{
return [
new Assert\NotBlank(),
new Assert\Type('string'),
new Assert\Length(['min' => 12]),
new Assert\NotCompromisedPassword(),
];
}
}
}

.. code-block:: php-attributes

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

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

#[\Attribute]
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(),
];
}
}

.. versionadded:: 5.2

The ability to use PHP attributes to configure constraints was introduced in
Symfony 5.2. Prior to this, Doctrine Annotations were the only way to
annotate constraints.

.. note::

The ``@Annotation`` or ``#[\Attribute]`` annotation is necessary for this new constraint in
order to make it available for use in classes via annotations.
Options for your constraint are represented as public properties on the
constraint class.

You can now use it anywhere you need it:

Expand All @@ -64,6 +103,19 @@ You can now use it anywhere you need it:
public $password;
}

.. code-block:: php-attributes

// 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
Expand Down