8000 [Validator] Document constraints as php 8 Attributes by wkania · Pull Request #14538 · symfony/symfony-docs · GitHub
[go: up one dir, main page]

Skip to content

[Validator] Document constraints as php 8 Attributes #14538

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

Closed
wants to merge 1 commit into from
Closed
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
13 changes: 13 additions & 0 deletions reference/constraints/Bic.rst
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,19 @@ will contain a Business Identifier Code (BIC).
protected $businessIdentifierCode;
}

.. code-block:: php-attributes

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

use Symfony\Component\Validator\Constraints as Assert;

class Transaction
{
#[Assert\Bic]
protected $businessIdentifierCode;
}

.. code-block:: yaml

# config/validator/validation.yaml
Expand Down
13 changes: 13 additions & 0 deletions reference/constraints/Blank.rst
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,19 @@ of an ``Author`` class were blank, you could do the following:
protected $firstName;
}

.. code-block:: php-attributes

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

use Symfony\Component\Validator\Constraints as Assert;

class Author
{
#[Assert\Blank]
protected $firstName;
}

.. code-block:: yaml

# config/validator/validation.yaml
Expand Down
29 changes: 29 additions & 0 deletions reference/constraints/Callback.rst
67F4
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,23 @@ Configuration
}
}

.. code-block:: php-attributes

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

use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Component\Validator\Context\ExecutionContextInterface;

class Author
{
#[Assert\Callback]
public function validate(ExecutionContextInterface $context, $payload)
{
// ...
}
}

.. code-block:: yaml

# config/validator/validation.yaml
Expand Down Expand Up @@ -178,6 +195,18 @@ You can then use the following configuration to invoke this validator:
{
}

.. code-block:: php-attributes

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

use Symfony\Component\Validator\Constraints as Assert;

#[Assert\Callback(["Acme\Validator", "validate"])]
class Author
{
}

.. code-block:: yaml

# config/validator/validation.yaml
Expand Down
16 changes: 16 additions & 0 deletions reference/constraints/CardScheme.rst
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,22 @@ on an object that will contain a credit card number.
protected $cardNumber;
}

.. code-block:: php-attributes

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

use Symfony\Component\Validator\Constraints as Assert;

class Transaction
{
#[Assert\CardScheme(
schemes: ["VISA"],
message: "Your credit card number is invalid."
)]
protected $cardNumber;
}

.. code-block:: yaml

# config/validator/validation.yaml
Expand Down
47 changes: 47 additions & 0 deletions reference/constraints/Choice.rst
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,27 @@ If your valid choice list is simple, you can pass them in directly via the
protected $genre;
}

.. code-block:: php-attributes

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

use Symfony\Component\Validator\Constraints as Assert;

class Author
{
const GENRES = ['fiction', 'non-fiction'];

#[Assert\Choice(["New York", "Berlin", "Tokyo"])]
protected $city;

/**
* You can also directly provide an array constant to the "choices" option in the annotation
*/
#[Assert\Choice(choices: Conference::GENRES, message: "Choose a valid genre.")]
protected $genre;
}

.. code-block:: yaml

# config/validator/validation.yaml
Expand Down Expand Up @@ -160,6 +181,19 @@ constraint.
protected $genre;
}

.. code-block:: php-attributes

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

use Symfony\Component\Validator\Constraints as Assert;

class Author
{
#[Assert\Choice(callback: "getGenres")]
protected $genre;
}

.. code-block:: yaml

# config/validator/validation.yaml
Expand Down Expand Up @@ -225,6 +259,19 @@ you can pass the class name and the method as an array.
protected $genre;
}

.. code-block:: php-attributes

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

use Symfony\Component\Validator\Constraints as Assert;

class Author
{
#[Assert\Choice(callback: ["App\Entity\Genre", "getGenres"])]
protected $genre;
}

.. code-block:: yaml

# config/validator/validation.yaml
Expand Down
18 changes: 18 additions & 0 deletions reference/constraints/Count.rst
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,24 @@ you might add the following:
protected $emails = [];
}

.. code-block:: php-attributes

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

use Symfony\Component\Validator\Constraints as Assert;

class Participant
{
#[Assert\Count(
min: 1,
max: 5,
minMessage: "You must specify at least one email",
maxMessage: "You cannot specify more than {{ limit }} emails"
)]
protected $emails = [];
}

.. code-block:: yaml

# config/validator/validation.yaml
Expand Down
13 changes: 13 additions & 0 deletions reference/constraints/Country.rst
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,19 @@ Basic Usage
protected $country;
}

.. code-block:: php-attributes

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

use Symfony\Component\Validator\Constraints as Assert;

class User
{
#[Assert\Country]
protected $country;
}

.. code-block:: yaml

# config/validator/validation.yaml
Expand Down
13 changes: 13 additions & 0 deletions reference/constraints/Currency.rst
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,19 @@ a valid currency, you could do the following:
protected $currency;
}

.. code-block:: php-attributes

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

use Symfony\Component\Validator\Constraints as Assert;

class Order
{
#[Assert\Currency]
protected $currency;
}

.. code-block:: yaml

# config/validator/validation.yaml
Expand Down
13 changes: 13 additions & 0 deletions reference/constraints/Date.rst
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,19 @@ Basic Usage
protected $birthday;
}

.. code-block:: php-attributes

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

use Symfony\Component\Validator\Constraints as Assert;

class Author
{
#[Assert\Date]
protected $birthday;
}

.. code-block:: yaml

# config/validator/validation.yaml
Expand Down
16 changes: 16 additions & 0 deletions reference/constraints/DateTime.rst
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,22 @@ Basic Usage
protected $createdAt;
}

.. code-block:: php-attributes

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

use Symfony\Component\Validator\Constraints as Assert;

class Author
{
/**
* @var string A "Y-m-d H:i:s" formatted value
*/
#[Assert\DateTime]
protected $createdAt;
}

.. code-block:: yaml

# config/validator/validation.yaml
Expand Down
18 changes: 18 additions & 0 deletions reference/constraints/DivisibleBy.rst
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,24 @@ The following constraints ensure that:
protected $quantity;
}

.. code-block:: php-attributes

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

use Symfony\Component\Validator\Constraints as Assert;

class Item
{
#[Assert\DivisibleBy(0.25)]
protected $weight;

#[Assert\DivisibleBy(
value: 5
)]
protected $quantity;
}

.. code-block:: yaml

# config/validator/validation.yaml
Expand Down
15 changes: 15 additions & 0 deletions reference/constraints/Email.rst
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,21 @@ Basic Usage
protected $email;
}

.. code-block:: php-attributes

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

use Symfony\Component\Validator\Constraints as Assert;

class Author
{
#[Assert\Email(
message: "The email '{{ value }}' is not a valid email."
)]
protected $email;
}

.. code-block:: yaml

# config/validator/validation.yaml
Expand Down
18 changes: 18 additions & 0 deletions reference/constraints/EqualTo.rst
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,24 @@ and that the ``age`` is ``20``, you could do the following:
protected $age;
}

.. code-block:: php-attributes

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

use Symfony\Component\Validator\Constraints as Assert;

class Person
{
#[Assert\EqualTo("Mary")]
protected $firstName;

#[Assert\EqualTo(
value: 20
)]
protected $age;
}

.. code-block:: yaml

# config/validator/validation.yaml
Expand Down
Loading
0