Skip to content

[Validator] Add attributes documentation of composite constraints #15541

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
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
22 changes: 22 additions & 0 deletions reference/constraints/All.rst
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,23 @@ entry in that array:
protected $favoriteColors = [];
}

.. code-block:: php-attributes

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

use Symfony\Component\Validator\Constraints as Assert;

// IMPORTANT: nested attributes requires PHP 8.1 or higher
class User
{
#[Assert\All([
new Assert\NotBlank,
new Assert\Length(min: 5),
])]
protected $favoriteColors = [];
}

.. code-block:: yaml

# config/validator/validation.yaml
Expand Down Expand Up @@ -93,6 +110,11 @@ entry in that array:
}
}

.. versionadded:: 5.4

The ``#[All]`` PHP attribute was introduced in Symfony 5.4 and requires
PHP 8.1 (which added nested attribute support).

Now, each entry in the ``favoriteColors`` array will be validated to not
be blank and to be at least 5 characters long.

Expand Down
30 changes: 30 additions & 0 deletions reference/constraints/AtLeastOneOf.rst
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,31 @@ The following constraints ensure that:
protected $grades;
}

.. code-block:: php-attributes

// src/Entity/Student.php
namespace App\Entity;

use Symfony\Component\Validator\Constraints as Assert;

// IMPORTANT: nested attributes requires PHP 8.1 or higher
class Student
{
#[Assert\AtLeastOneOf([
new Assert\Regex('/#/'),
new Assert\Length(min: 10),
])]
protected $plainPassword;

#[Assert\AtLeastOneOf([
new Assert\Count(min: 3),
new Assert\All(
new Assert\GreaterThanOrEqual(5)
),
])]
protected $grades;
}

.. code-block:: yaml

# config/validator/validation.yaml
Expand Down Expand Up @@ -149,6 +174,11 @@ The following constraints ensure that:
}
}

.. versionadded:: 5.4

The ``#[AtLeastOneOf]`` PHP attribute was introduced in Symfony 5.4 and
requires PHP 8.1 (which added nested attribute support).

Options
-------

Expand Down
34 changes: 34 additions & 0 deletions reference/constraints/Collection.rst
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,35 @@ following:
];
}

.. code-block:: php-attributes

// src/Entity/Author.php
namespace App\Entity;

use Symfony\Component\Validator\Constraints as Assert;

// IMPORTANT: nested attributes requires PHP 8.1 or higher
class Author
{
#[Assert\Collection(
fields: [
'personal_email' => new Assert\Email,
'short_bio' => [
new Assert\NotBlank,
new Assert\Length(
max: 100,
maxMessage: 'Your short bio is too long!'
)
]
],
allowMissingFields: true,
)]
protected $profileData = [
'personal_email' => '...',
'short_bio' => '...',
];
}

.. code-block:: yaml

# config/validator/validation.yaml
Expand Down Expand Up @@ -162,6 +191,11 @@ following:
}
}

.. versionadded:: 5.4

The ``#[Collection]`` PHP attribute was introduced in Symfony 5.4 and
requires PHP 8.1 (which added nested attribute support).

Presence and Absence of Fields
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Expand Down
26 changes: 26 additions & 0 deletions reference/constraints/Sequentially.rst
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,27 @@ You can validate each of these constraints sequentially to solve these issues:
public $address;
}

.. code-block:: php-attributes

// src/Localization/Place.php
namespace App\Localization;

use App\Validator\Constraints as AcmeAssert;
use Symfony\Component\Validator\Constraints as Assert;

// IMPORTANT: nested attributes requires PHP 8.1 or higher
class Place
{
#[Assert\Sequentially([
new Assert\NotNull,
new Assert\Type('string'),
new Assert\Length(min: 10),
new Assert\Regex(Place::ADDRESS_REGEX),
new AcmeAssert\Geolocalizable,
])]
public $address;
}

.. code-block:: yaml

# config/validator/validation.yaml
Expand Down Expand Up @@ -128,6 +149,11 @@ You can validate each of these constraints sequentially to solve these issues:
}
}

.. versionadded:: 5.4

The ``#[Sequentially]`` PHP attribute was introduced in Symfony 5.4 and
requires PHP 8.1 (which added nested attribute support).

Options
-------

Expand Down