Skip to content

Commit 41fa05e

Browse files
committed
minor #11366 streamline the constraints reference (OskarStark)
This PR was merged into the 3.4 branch. Discussion ---------- streamline the constraints reference I felt the constraints need some love 💟 Commits ------- ce77fdb streamline the constraints reference
2 parents 7aafce9 + ce77fdb commit 41fa05e

29 files changed

+57
-71
lines changed

reference/constraints/All.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ entry in that array:
3939
* @Assert\Length(min=5)
4040
* })
4141
*/
42-
protected $favoriteColors = [];
42+
protected $favoriteColors = [];
4343
}
4444
4545
.. code-block:: yaml

reference/constraints/Bic.rst

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,6 @@ will contain a Business Identifier Code (BIC).
7373
7474
class Transaction
7575
{
76-
protected $businessIdentifierCode;
77-
7876
public static function loadValidatorMetadata(ClassMetadata $metadata)
7977
{
8078
$metadata->addPropertyConstraint('businessIdentifierCode', new Assert\Bic());

reference/constraints/Callback.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,11 @@ Configuration
8787
{
8888
$metadata->addConstraint(new Assert\Callback('validate'));
8989
}
90+
91+
public function validate(ExecutionContextInterface $context, $payload)
92+
{
93+
// ...
94+
}
9095
}
9196
9297
The Callback Method

reference/constraints/CardScheme.rst

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,8 +84,6 @@ on an object that will contain a credit card number.
8484
8585
class Transaction
8686
{
87-
protected $cardNumber;
88-
8987
public static function loadValidatorMetadata(ClassMetadata $metadata)
9088
{
9189
$metadata->addPropertyConstraint('cardNumber', new Assert\CardScheme([

reference/constraints/Choice.rst

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,8 +112,6 @@ If your valid choice list is simple, you can pass them in directly via the
112112
113113
class Author
114114
{
115-
protected $genre;
116-
117115
public static function loadValidatorMetadata(ClassMetadata $metadata)
118116
{
119117
$metadata->addPropertyConstraint(

reference/constraints/Collection.rst

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -141,8 +141,6 @@ following:
141141
142142
class Author
143143
{
144-
private $options = [];
145-
146144
public static function loadValidatorMetadata(ClassMetadata $metadata)
147145
{
148146
$metadata->addPropertyConstraint('profileData', new Assert\Collection([
@@ -208,7 +206,7 @@ you can do the following:
208206
* }
209207
* )
210208
*/
211-
protected $profileData = ['personal_email'];
209+
protected $profileData = ['personal_email'];
212210
}
213211
214212
.. code-block:: yaml
@@ -272,9 +270,10 @@ you can do the following:
272270
{
273271
$metadata->addPropertyConstraint('profileData', new Assert\Collection([
274272
'fields' => [
275-
'personal_email' => new Assert\Required(
276-
[new Assert\NotBlank(), new Assert\Email()]
277-
),
273+
'personal_email' => new Assert\Required([
274+
new Assert\NotBlank(),
275+
new Assert\Email(),
276+
]),
278277
'alternate_email' => new Assert\Optional(new Assert\Email()),
279278
],
280279
]));

reference/constraints/Count.rst

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ you might add the following:
4545
* maxMessage = "You cannot specify more than {{ limit }} emails"
4646
* )
4747
*/
48-
protected $emails = [];
48+
protected $emails = [];
4949
}
5050
5151
.. code-block:: yaml
@@ -93,8 +93,8 @@ you might add the following:
9393
public static function loadValidatorMetadata(ClassMetadata $metadata)
9494
{
9595
$metadata->addPropertyConstraint('emails', new Assert\Count([
96-
'min' => 1,
97-
'max' => 5,
96+
'min' => 1,
97+
'max' => 5,
9898
'minMessage' => 'You must specify at least one email',
9999
'maxMessage' => 'You cannot specify more than {{ limit }} emails',
100100
]));

reference/constraints/Country.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ Basic Usage
3232
/**
3333
* @Assert\Country
3434
*/
35-
protected $country;
35+
protected $country;
3636
}
3737
3838
.. code-block:: yaml

reference/constraints/Date.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ Basic Usage
3434
/**
3535
* @Assert\Date
3636
*/
37-
protected $birthday;
37+
protected $birthday;
3838
}
3939
4040
.. code-block:: yaml

reference/constraints/DateTime.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ Basic Usage
3535
/**
3636
* @Assert\DateTime
3737
*/
38-
protected $createdAt;
38+
protected $createdAt;
3939
}
4040
4141
.. code-block:: yaml

reference/constraints/Email.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ Basic Usage
3939
* checkMX = true
4040
* )
4141
*/
42-
protected $email;
42+
protected $email;
4343
}
4444
4545
.. code-block:: yaml

reference/constraints/Image.rst

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -211,13 +211,11 @@ following code:
211211
212212
class Author
213213
{
214-
// ...
215-
216214
public static function loadValidatorMetadata(ClassMetadata $metadata)
217215
{
218216
$metadata->addPropertyConstraint('headshot', new Assert\Image([
219-
'allowLandscape' => false,
220-
'allowPortrait' => false,
217+
'allowLandscape' => false,
218+
'allowPortrait' => false,
221219
]));
222220
}
223221
}

reference/constraints/Ip.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ Basic Usage
3535
/**
3636
* @Assert\Ip
3737
*/
38-
protected $ipAddress;
38+
protected $ipAddress;
3939
}
4040
4141
.. code-block:: yaml

reference/constraints/IsFalse.rst

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,10 +55,10 @@ method returns **false**:
5555
* message = "You've entered an invalid state."
5656
* )
5757
*/
58-
public function isStateInvalid()
59-
{
58+
public function isStateInvalid()
59+
{
6060
// ...
61-
}
61+
}
6262
}
6363
6464
.. code-block:: yaml
@@ -99,10 +99,17 @@ method returns **false**:
9999
{
100100
public static function loadValidatorMetadata(ClassMetadata $metadata)
101101
{
102-
$metadata->addGetterConstraint('stateInvalid', new Assert\IsFalse());
102+
$metadata->addGetterConstraint('stateInvalid', new Assert\IsFalse([
103+
'message' => 'You've entered an invalid state.',
104+
]));
103105
}
104106
}
105107
108+
public function isStateInvalid()
109+
{
110+
// ...
111+
}
112+
106113
Options
107114
-------
108115

reference/constraints/IsTrue.rst

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ Then you can constrain this method with ``IsTrue``.
5656
protected $token;
5757
5858
/**
59-
* @Assert\IsTrue(message="The token is invalid")
59+
* @Assert\IsTrue(message="The token is invalid.")
6060
*/
6161
public function isTokenValid()
6262
{
@@ -100,8 +100,6 @@ Then you can constrain this method with ``IsTrue``.
100100
101101
class Author
102102
{
103-
protected $token;
104-
105103
public static function loadValidatorMetadata(ClassMetadata $metadata)
106104
{
107105
$metadata->addGetterConstraint('tokenValid', new IsTrue([

reference/constraints/Isbn.rst

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -84,12 +84,10 @@ on an object that will contain an ISBN.
8484
8585
class Book
8686
{
87-
protected $isbn;
88-
8987
public static function loadValidatorMetadata(ClassMetadata $metadata)
9088
{
9189
$metadata->addPropertyConstraint('isbn', new Assert\Isbn([
92-
'type' => 'isbn10',
90+
'type' => 'isbn10',
9391
'message' => 'This value is not valid.',
9492
]));
9593
}

reference/constraints/Issn.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ Basic Usage
3535
/**
3636
* @Assert\Issn
3737
*/
38-
protected $issn;
38+
protected $issn;
3939
}
4040
4141
.. code-block:: yaml
@@ -87,7 +87,7 @@ Options
8787
message
8888
~~~~~~~
8989

90-
**type**: ``String`` default: ``This value is not a valid ISSN.``
90+
**type**: ``string`` default: ``This value is not a valid ISSN.``
9191

9292
The message shown if the given value is not a valid ISSN.
9393

reference/constraints/Language.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ Basic Usage
3333
/**
3434
* @Assert\Language
3535
*/
36-
protected $preferredLanguage;
36+
protected $preferredLanguage;
3737
}
3838
3939
.. code-block:: yaml

reference/constraints/Length.rst

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ and "50", you might add the following:
5252
* maxMessage = "Your first name cannot be longer than {{ limit }} characters"
5353
* )
5454
*/
55-
protected $firstName;
55+
protected $firstName;
5656
}
5757
5858
.. code-block:: yaml
@@ -104,8 +104,8 @@ and "50", you might add the following:
104104
public static function loadValidatorMetadata(ClassMetadata $metadata)
105105
{
106106
$metadata->addPropertyConstraint('firstName', new Assert\Length([
107-
'min' => 2,
108-
'max' => 50,
107+
'min' => 2,
108+
'max' => 50,
109109
'minMessage' => 'Your first name must be at least {{ limit }} characters long',
110110
'maxMessage' => 'Your first name cannot be longer than {{ limit }} characters',
111111
]));

reference/constraints/Locale.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ Basic Usage
3737
/**
3838
* @Assert\Locale
3939
*/
40-
protected $locale;
40+
protected $locale;
4141
}
4242
4343
.. code-block:: yaml

reference/constraints/Range.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,8 +92,8 @@ you might add the following:
9292
public static function loadValidatorMetadata(ClassMetadata $metadata)
9393
{
9494
$metadata->addPropertyConstraint('height', new Assert\Range([
95-
'min' => 120,
96-
'max' => 180,
95+
'min' => 120,
96+
'max' => 180,
9797
'minMessage' => 'You must be at least {{ limit }}cm tall to enter',
9898
'maxMessage' => 'You cannot be taller than {{ limit }}cm to enter',
9999
]));

reference/constraints/Regex.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ it a custom message:
156156
{
157157
$metadata->addPropertyConstraint('firstName', new Assert\Regex([
158158
'pattern' => '/\d/',
159-
'match' => false,
159+
'match' => false,
160160
'message' => 'Your name cannot contain a number',
161161
]));
162162
}
@@ -257,7 +257,7 @@ need to specify the HTML5 compatible pattern in the ``htmlPattern`` option:
257257
public static function loadValidatorMetadata(ClassMetadata $metadata)
258258
{
259259
$metadata->addPropertyConstraint('name', new Assert\Regex([
260-
'pattern' => '/^[a-z]+$/i',
260+
'pattern' => '/^[a-z]+$/i',
261261
'htmlPattern' => '^[a-zA-Z]+$',
262262
]));
263263
}

reference/constraints/Time.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ of the day when the event starts:
3737
/**
3838
* @Assert\Time
3939
*/
40-
protected $startsAt;
40+
protected $startsAt;
4141
}
4242
4343
.. code-block:: yaml

reference/constraints/Type.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ This will check if ``firstName`` is of type ``string`` and that ``age`` is an
100100
$metadata->addPropertyConstraint('firstName', new Assert\Type('string'));
101101
102102
$metadata->addPropertyConstraint('age', new Assert\Type([
103-
'type' => 'integer',
103+
'type' => 'integer',
104104
'message' => 'The value {{ value }} is not a valid {{ type }}.',
105105
]));
106106
}

reference/constraints/UniqueEntity.rst

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -51,14 +51,10 @@ your user table:
5151
class Author
5252
{
5353
/**
54-
* @var string $email
55-
*
5654
* @ORM\Column(name="email", type="string", length=255, unique=true)
5755
* @Assert\Email
5856
*/
5957
protected $email;
60-
61-
// ...
6258
}
6359
6460
.. code-block:: yaml
@@ -104,7 +100,7 @@ your user table:
104100
public static function loadValidatorMetadata(ClassMetadata $metadata)
105101
{
106102
$metadata->addConstraint(new UniqueEntity([
107-
'fields' => 'email',
103+
'fields' => 'email',
108104
]));
109105
110106
$metadata->addPropertyConstraint('email', new Assert\Email());
@@ -285,9 +281,9 @@ Consider this example:
285281
public static function loadValidatorMetadata(ClassMetadata $metadata)
286282
{
287283
$metadata->addConstraint(new UniqueEntity([
288-
'fields' => ['host', 'port'],
284+
'fields' => ['host', 'port'],
289285
'errorPath' => 'port',
290-
'message' => 'This port is already in use on that host.',
286+
'message' => 'This port is already in use on that host.',
291287
]));
292288
}
293289
}

0 commit comments

Comments
 (0)