8000 [Validator] Document constraints as php 8 Attributes · symfony/symfony-docs@738af4f · GitHub
[go: up one dir, main page]

Skip to content

Commit 738af4f

Browse files
committed
[Validator] Document constraints as php 8 Attributes
1 parent 18373c8 commit 738af4f

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

56 files changed

+1354
-8
lines changed

reference/constraints/Bic.rst

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,19 @@ will contain a Business Identifier Code (BIC).
4141
protected $businessIdentifierCode;
4242
}
4343
44+
.. code-block:: php-attributes
45+
46+
// src/Entity/Transaction.php
47+
namespace App\Entity;
48+
49+
use Symfony\Component\Validator\Constraints as Assert;
50+
51+
class Transaction
52+
{
53+
#[Assert\Bic]
54+
protected $businessIdentifierCode;
55+
}
56+
4457
.. code-block:: yaml
4558
4659
# config/validator/validation.yaml

reference/constraints/Blank.rst

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,19 @@ of an ``Author`` class were blank, you could do the following:
4545
protected $firstName;
4646
}
4747
48+
.. code-block:: php-attributes
49< F438 /td>+
50+
// src/Entity/Author.php
51+
namespace App\Entity;
52+
53+
use Symfony\Component\Validator\Constraints as Assert;
54+
55+
class Author
56+
{
57+
#[Assert\Blank]
58+
protected $firstName;
59+
}
60+
4861
.. code-block:: yaml
4962
5063
# config/validator/validation.yaml

reference/constraints/Callback.rst

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,23 @@ Configuration
5050
}
5151
}
5252
53+
.. code-block:: php-attributes
54+
55+
// src/Entity/Author.php
56+
namespace App\Entity;
57+
58+
use Symfony\Component\Validator\Constraints as Assert;
59+
use Symfony\Component\Validator\Context\ExecutionContextInterface;
60+
61+
class Author
62+
{
63+
#[Assert\Callback]
64+
public function validate(ExecutionContextInterface $context, $payload)
65+
{
66+
// ...
67+
}
68+
}
69+
5370
.. code-block:: yaml
5471
5572
# config/validator/validation.yaml
@@ -178,6 +195,18 @@ You can then use the following configuration to invoke this validator:
178195
{
179196
}
180197
198+
.. code-block:: php-attributes
199+
200+
// src/Entity/Author.php
201+
namespace App\Entity;
202+
203+
use Symfony\Component\Validator\Constraints as Assert;
204+
205+
#[Assert\Callback(["Acme\Validator", "validate"])]
206+
class Author
207+
{
208+
}
209+
181210
.. code-block:: yaml
182211
183212
# config/validator/validation.yaml

reference/constraints/CardScheme.rst

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,22 @@ on an object that will contain a credit card number.
4141
protected $cardNumber;
4242
}
4343
44+
.. code-block:: php-attributes
45+
46+
// src/Entity/Transaction.php
47+
namespace App\Entity;
48+
49+
use Symfony\Component\Validator\Constraints as Assert;
50+
51+
class Transaction
52+
{
53+
#[Assert\CardScheme(
54+
schemes: ["VISA"],
55+
message: "Your credit card number is invalid."
56+
)]
57+
protected $cardNumber;
58+
}
59+
4460
.. code-block:: yaml
4561
4662
# config/validator/validation.yaml

reference/constraints/Choice.rst

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,27 @@ If your valid choice list is simple, you can pass them in directly via the
5858
protected $genre;
5959
}
6060
61+
.. code-block:: php-attributes
62+
63+
// src/Entity/Author.php
64+
namespace App\Entity;
65+
66+
use Symfony\Component\Validator\Constraints as Assert;
67+
68+
class Author
69+
{
70+
const GENRES = ['fiction', 'non-fiction'];
71+
72+
#[Assert\Choice(["New York", "Berlin", "Tokyo"])]
73+
protected $city;
74+
75+
/**
76+
* You can also directly provide an array constant to the "choices" option in the annotation
77+
*/
78+
#[Assert\Choice(choices: Conference::GENRES, message: "Choose a valid genre.")]
79+
protected $genre;
80+
}
81+
6182
.. code-block:: yaml
6283
6384
# config/validator/validation.yaml
@@ -160,6 +181,19 @@ constraint.
160181
protected $genre;
161182
}
162183
184+
.. code-block:: php-attributes
185+
186+
// src/Entity/Author.php
187+
namespace App\Entity;
188+
189+
use Symfony\Component\Validator\Constraints as Assert;
190+
191+
class Author
192+
{
193+
#[Assert\Choice(callback: "getGenres")]
194+
protected $genre;
195+
}
196+
163197
.. code-block:: yaml
164198
165199
# config/validator/validation.yaml
@@ -225,6 +259,19 @@ you can pass the class name and the method as an array.
225259
protected $genre;
226260
}
227261
262+
.. code-block:: php-attributes
263+
264+
// src/Entity/Author.php
265+
namespace App\Entity;
266+
267+
use Symfony\Component\Validator\Constraints as Assert;
268+
269+
class Author
270+
{
271+
#[Assert\Choice(callback: ["App\Entity\Genre", "getGenres"])]
272+
protected $genre;
273+
}
274+
228275
.. code-block:: yaml
229276
230277
# config/validator/validation.yaml

reference/constraints/Count.rst

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,24 @@ you might add the following:
4747
protected $emails = [];
4848
}
4949
50+
.. code-block:: php-attributes
51+
52+
// src/Entity/Participant.php
53+
namespace App\Entity;
54+
55+
use Symfony\Component\Validator\Constraints as Assert;
56+
57+
class Participant
58+
{
59+
#[Assert\Count(
60+
min: 1,
61+
max: 5,
62+
minMessage: "You must specify at least one email",
63+
maxMessage: "You cannot specify more than {{ limit }} emails"
64+
)]
65+
protected $emails = [];
66+
}
67+
5068
.. code-block:: yaml
5169
5270
# config/validator/validation.yaml

reference/constraints/Country.rst

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,19 @@ Basic Usage
3333
protected $country;
3434
}
3535
36+
.. code-block:: php-attributes
37+
38+
// src/Entity/User.php
39+
namespace App\Entity;
40+
41+
use Symfony\Component\Validator\Constraints as Assert;
42+
43+
class User
44+
{
45+
#[Assert\Country]
46+
protected $country;
47+
}
48+
3649
.. code-block:: yaml
3750
3851
# config/validator/validation.yaml

reference/constraints/Currency.rst

Lines changed: 13 additions & 0 deletions
Original file line n 10000 umberDiff line numberDiff line change
@@ -35,6 +35,19 @@ a valid currency, you could do the following:
3535
protected $currency;
3636
}
3737
38+
.. code-block:: php-attributes
39+
40+
// src/Entity/Order.php
41+
namespace App\Entity;
42+
43+
use Symfony\Component\Validator\Constraints as Assert;
44+
45+
class Order
46+
{
47+
#[Assert\Currency]
48+
protected $currency;
49+
}
50+
3851
.. code-block:: yaml
3952
4053
# config/validator/validation.yaml

reference/constraints/Date.rst

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,19 @@ Basic Usage
3434
protected $birthday;
3535
}
3636
37+
.. code-block:: php-attributes
38+
39+
// src/Entity/Author.php
40+
namespace App\Entity;
41+
42+
use Symfony\Component\Validator\Constraints as Assert;
43+
44+
class Author
45+
{
46+
#[Assert\Date]
47+
protected $birthday;
48+
}
49+
3750
.. code-block:: yaml
3851
3952
# config/validator/validation.yaml

reference/constraints/DateTime.rst

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,22 @@ Basic Usage
3535
protected $createdAt;
3636
}
3737
38+
.. code-block:: php-attributes
39+
40+
// src/Entity/Author.php
41+
namespace App\Entity;
42+
43+
use Symfony\Component\Validator\Constraints as Assert;
44+
45+
class Author
46+
{
47+
/**
48+
* @var string A "Y-m-d H:i:s" formatted value
49+
*/
50+
#[Assert\DateTime]
51+
protected $createdAt;
52+
}
53+
3854
.. code-block:: yaml
3955
4056
# config/validator/validation.yaml

reference/constraints/DivisibleBy.rst

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ The following constraints ensure that:
3939
4040
class Item
4141
{
42-
4342
/**
4443
* @Assert\DivisibleBy(0.25)
4544
*/
@@ -53,6 +52,24 @@ The following constraints ensure that:
5352
protected $quantity;
5453
}
5554
55+
.. code-block:: php-attributes
56+
57+
// src/Entity/Item.php
58+
namespace App\Entity;
59+
60+
use Symfony\Component\Validator\Constraints as Assert;
61+
62+
class Item
63+
{
64+
#[Assert\DivisibleBy(0.25)]
65+
protected $weight;
66+
67+
#[Assert\DivisibleBy(
68+
value: 5
69+
)]
70+
protected $quantity;
71+
}
72+
5673
.. code-block:: yaml
5774
5875
# config/validator/validation.yaml

reference/constraints/Email.rst

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,21 @@ Basic Usage
3737
protected $email;
3838
}
3939
40+
.. code-block:: php-attributes
41+
42+
// src/Entity/Author.php
43+
namespace App\Entity;
44+
45+
use Symfony\Component\Validator\Constraints as Assert;
46+
47+
class Author
48+
{
49+
#[Assert\Email(
50+
message: "The email '{{ value }}' is not a valid email."
51+
)]
52+
protected $email;
53+
}
54+
4055
.. code-block:: yaml
4156
4257
# config/validator/validation.yaml

reference/constraints/EqualTo.rst

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,24 @@ and that the ``age`` is ``20``, you could do the following:
5252
protected $age;
5353
}
5454
55+
.. code-block:: php-attributes
56+
57+
// src/Entity/Person.php
58+
namespace App\Entity;
59+
60+
use Symfony\Component\Validator\Constraints as Assert;
61+
62+
class Person
63+
{
64+
#[Assert\EqualTo("Mary")]
65+
protected $firstName;
66+
67+
#[Assert\EqualTo(
68+
value: 20
69+
)]
70+
protected $age;
71+
}
72+
5573
.. code-block:: yaml
5674
5775
# config/validator/validation.yaml

0 commit comments

Comments
 (0)
0