Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 7b23335

Browse files
committed
feature #14544 [Validator] Document constraints as php 8 Attributes (wkania)
This PR was squashed before being merged into the 5.2 branch. Discussion ---------- [Validator] Document constraints as php 8 Attributes We already have [examples](https://github.com/symfony/symfony-docs/pull/14230/files) of code for the PHP attributes. We also have [Constraints as php 8 Attributes](symfony/symfony#38309) and [2](#14305). The rest will be implemented in the [future](symfony/symfony#38503). Commits ------- 748bd54 [Validator] Document constraints as php 8 Attributes
2 parents 19d170e + 748bd54 commit 7b23335

Some content is hidden

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

58 files changed

+1404
-0
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+
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: 30 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,19 @@ 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 Acme\Validator;
204+
use Symfony\Component\Validator\Constraints as Assert;
205+
206+
#[Assert\Callback([Validator::class, 'validate'])]
207+
class Author
208+
{
209+
}
210+
181211
.. code-block:: yaml
182212
183213
# 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: [Assert\CardScheme::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: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,24 @@ 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+
#[Assert\Choice(choices: Author::GENRES, message: 'Choose a valid genre.')]
76+
protected $genre;
77+
}
78+
6179
.. code-block:: yaml
6280
6381
# config/validator/validation.yaml
@@ -160,6 +178,19 @@ constraint.
160178
protected $genre;
161179
}
162180
181+
.. code-block:: php-attributes
182+
183+
// src/Entity/Author.php
184+
namespace App\Entity;
185+
186+
use Symfony\Component\Validator\Constraints as Assert;
187+
188+
class Author
189+
{
190+
#[Assert\Choice(callback: 'getGenres')]
191+
protected $genre;
192+
}
193+
163194
.. code-block:: yaml
164195
165196
# config/validator/validation.yaml
@@ -225,6 +256,20 @@ you can pass the class name and the method as an array.
225256
protected $genre;
226257
}
227258
259+
.. code-block:: php-attributes
260+
261+
// src/Entity/Author.php
262+
namespace App\Entity;
263+
264+
use App\Entity\Genre
265+
use Symfony\Component\Validator\Constraints as Assert;
266+
267+
class Author
268+
{
269+
#[Assert\Choice(callback: [Genre::class, 'getGenres'])]
270+
protected $genre;
271+
}
272+
228273
.. code-block:: yaml
229274
230275
# 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 numberDiff 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 & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,24 @@ The following constraints ensure that:
5252
protected $quantity;
5353
}
5454
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+
5573
.. code-block:: yaml
5674
5775
# 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)