Skip to content

Commit 28c1351

Browse files
committed
Merge pull request #2083 from WouterJ/add_ref_php_format
Added constraints PHP format and some code tweaks
2 parents b811c1b + 9cabf0d commit 28c1351

28 files changed

+534
-134
lines changed

reference/constraints/All.rst

Lines changed: 38 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -34,21 +34,21 @@ entry in that array:
3434
3535
.. code-block:: php-annotations
3636
37-
// src/Acme/UserBundle/Entity/User.php
38-
namespace Acme\UserBundle\Entity;
39-
40-
use Symfony\Component\Validator\Constraints as Assert;
41-
42-
class User
43-
{
44-
/**
45-
* @Assert\All({
46-
* @Assert\NotBlank
47-
* @Assert\MinLength(5),
48-
* })
49-
*/
50-
protected $favoriteColors = array();
51-
}
37+
// src/Acme/UserBundle/Entity/User.php
38+
namespace Acme\UserBundle\Entity;
39+
40+
use Symfony\Component\Validator\Constraints as Assert;
41+
42+
class User
43+
{
44+
/**
45+
* @Assert\All({
46+
* @Assert\NotBlank
47+
* @Assert\MinLength(5),
48+
* })
49+
*/
50+
protected $favoriteColors = array();
51+
}
5252
5353
.. code-block:: xml
5454
@@ -58,14 +58,35 @@ entry in that array:
5858
<constraint name="All">
5959
<option name="constraints">
6060
<constraint name="NotBlank" />
61-
<constraint name="Length">
62-
<option name="min">5</option>
61+
<constraint name="MinLength">
62+
<option name="limit">5</option>
6363
</constraint>
6464
</option>
6565
</constraint>
6666
</property>
6767
</class>
6868
69+
.. code-block:: php
70+
71+
// src/Acme/UserBundle/Enttiy/User.php
72+
namespace Acme\UserBundle\Entity;
73+
74+
use Symfony\Component\Validator\Mapping\ClassMetadata;
75+
use Symfony\Component\Validator\Constraints as Assert;
76+
77+
class User
78+
{
79+
public static function loadValidatorMetadata(ClassMetadata $metadata)
80+
{
81+
$metadata->addPropertyConstraint('favoriteColors', new Assert\All(array(
82+
'constraints' => array(
83+
new Assert\NotBlank(),
84+
new Assert\MinLength(array('limit' => 5)),
85+
),
86+
)));
87+
}
88+
}
89+
6990
Now, each entry in the ``favoriteColors`` array will be validated to not
7091
be blank and to be at least 5 characters long.
7192

reference/constraints/Blank.rst

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,17 @@ of an ``Author`` class were blank, you could do the following:
2626

2727
.. code-block:: yaml
2828
29-
properties:
30-
firstName:
31-
- Blank: ~
29+
# src/BlogBundle/Resources/config/validation.yml
30+
Acme\BlogBundle\Entity\Author:
31+
properties:
32+
firstName:
33+
- Blank: ~
3234
3335
.. code-block:: php-annotations
3436
3537
// src/Acme/BlogBundle/Entity/Author.php
38+
namespace Acme\BlogBundle\Entity;
39+
3640
use Symfony\Component\Validator\Constraints as Assert;
3741
3842
class Author
@@ -52,6 +56,22 @@ of an ``Author`` class were blank, you could do the following:
5256
</property>
5357
</class>
5458
59+
.. code-block:: php
60+
61+
// src/Acme/BlogBundle/Entity/Author.php
62+
namespace Acme\BlogBundle\Entity;
63+
64+
use Symfony\Component\Validator\Mapping\ClassMetadata;
65+
use Symfony\Component\Validator\Constraints as Assert;
66+
67+
class Author
68+
{
69+
public static function loadValidatorMetadata(ClassMetadata $metadata)
70+
{
71+
$metadata->addPropertyConstraint('firstName', new Assetc\Blank());
72+
}
73+
}
74+
5575
Options
5676
-------
5777

reference/constraints/Callback.rst

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ Setup
4343
.. code-block:: php-annotations
4444
4545
// src/Acme/BlogBundle/Entity/Author.php
46+
namespace Acme\BlogBundle\Entity;
47+
4648
use Symfony\Component\Validator\Constraints as Assert;
4749
4850
/**
@@ -63,6 +65,24 @@ Setup
6365
</constraint>
6466
</class>
6567
68+
.. code-block:: php
69+
70+
// src/Acme/BlogBundle/Entity/Author.php
71+
namespace Acme\BlogBundle\Entity;
72+
73+
use Symfony\Component\Validator\Mapping\ClassMetadata;
74+
use Symfony\Component\Validator\Constraints as Assert;
75+
76+
class Author
77+
{
78+
public static function loadValidatorMetadata(ClassMetadata $metadata)
79+
{
80+
$metadata->addConstraint(new Assert\Callback(array(
81+
'methods' => array('isAuthorValid'),
82+
)));
83+
}
84+
}
85+
6686
The Callback Method
6787
-------------------
6888

reference/constraints/Choice.rst

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,8 @@ If your valid choice list is simple, you can pass them in directly via the
6464
.. code-block:: php-annotations
6565
6666
// src/Acme/BlogBundle/Entity/Author.php
67+
namespace Acme\BlogBundle\Entity;
68+
6769
use Symfony\Component\Validator\Constraints as Assert;
6870
6971
class Author
@@ -77,16 +79,18 @@ If your valid choice list is simple, you can pass them in directly via the
7779
.. code-block:: php
7880
7981
// src/Acme/BlogBundle/EntityAuthor.php
82+
namespace Acme\BlogBundle\Entity;
83+
8084
use Symfony\Component\Validator\Mapping\ClassMetadata;
81-
use Symfony\Component\Validator\Constraints\Choice;
85+
use Symfony\Component\Validator\Constraints as Assert;
8286
8387
class Author
8488
{
8589
protected $gender;
8690
8791
public static function loadValidatorMetadata(ClassMetadata $metadata)
8892
{
89-
$metadata->addPropertyConstraint('gender', new Choice(array(
93+
$metadata->addPropertyConstraint('gender', new Assert\Choice(array(
9094
'choices' => array('male', 'female'),
9195
'message' => 'Choose a valid gender',
9296
)));
@@ -280,4 +284,4 @@ strict
280284

281285
If true, the validator will also check the type of the input value. Specifically,
282286
this value is passed to as the third argument to the PHP :phpfunction:`in_array` method
283-
when checking to see if a value is in the valid choices array.
287+
when checking to see if a value is in the valid choices array.

reference/constraints/Collection.rst

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -53,21 +53,25 @@ blank but is no longer than 100 characters in length, you would do the following
5353

5454
.. code-block:: yaml
5555
56-
properties:
57-
profileData:
58-
- Collection:
59-
fields:
60-
personal_email: Email
61-
short_bio:
62-
- NotBlank
63-
- MaxLength:
64-
limit: 100
65-
message: Your short bio is too long!
66-
allowMissingFields: true
56+
# src/BlogBundle/Resources/config/validation.yml
57+
Acme\BlogBundle\Entity\Author:
58+
properties:
59+
profileData:
60+
- Collection:
61+
fields:
62+
personal_email: Email
63+
short_bio:
64+
- NotBlank
65+
- MaxLength:
66+
limit: 100
67+
message: Your short bio is too long!
68+
allowMissingFields: true
6769
6870
.. code-block:: php-annotations
6971
7072
// src/Acme/BlogBundle/Entity/Author.php
73+
namespace Acme\BlogBundle\Entity;
74+
7175
use Symfony\Component\Validator\Constraints as Assert;
7276
7377
class Author
@@ -119,21 +123,21 @@ blank but is no longer than 100 characters in length, you would do the following
119123
.. code-block:: php
120124
121125
// src/Acme/BlogBundle/Entity/Author.php
126+
namespace Acme\BlogBundle\Entity;
127+
122128
use Symfony\Component\Validator\Mapping\ClassMetadata;
123-
use Symfony\Component\Validator\Constraints\Collection;
124-
use Symfony\Component\Validator\Constraints\Email;
125-
use Symfony\Component\Validator\Constraints\MaxLength;
129+
use Symfony\Component\Validator\Constraints as Assert;
126130
127131
class Author
128132
{
129133
private $options = array();
130134
131135
public static function loadValidatorMetadata(ClassMetadata $metadata)
132136
{
133-
$metadata->addPropertyConstraint('profileData', new Collection(array(
137+
$metadata->addPropertyConstraint('profileData', new Assert\Collection(array(
134138
'fields' => array(
135-
'personal_email' => new Email(),
136-
'lastName' => array(new NotBlank(), new MaxLength(100)),
139+
'personal_email' => new Assert\Email(),
140+
'lastName' => array(new Assert\NotBlank(), new Assert\MaxLength(100)),
137141
),
138142
'allowMissingFields' => true,
139143
)));

reference/constraints/Country.rst

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -28,18 +28,18 @@ Basic Usage
2828
2929
.. code-block:: php-annotations
3030
31-
// src/Acme/UserBundle/Entity/User.php
32-
namespace Acme\UserBundle\Entity;
33-
34-
use Symfony\Component\Validator\Constraints as Assert;
35-
36-
class User
37-
{
38-
/**
39-
* @Assert\Country
40-
*/
41-
protected $country;
42-
}
31+
// src/Acme/UserBundle/Entity/User.php
32+
namespace Acme\UserBundle\Entity;
33+
34+
use Symfony\Component\Validator\Constraints as Assert;
35+
36+
class User
37+
{
38+
/**
39+
* @Assert\Country
40+
*/
41+
protected $country;
42+
}
4343
4444
.. code-block:: xml
4545
@@ -50,6 +50,22 @@ Basic Usage
5050
</property>
5151
</class>
5252
53+
.. code-block:: php
54+
55+
// src/Acme/UserBundle/Entity/User.php
56+
namespace Acme\UserBundle\Entity;
57+
58+
use Symfony\Component\Validator\Mapping\ClassMetadata;
59+
use Symfony\Component\Validator\Constraints as Assert;
60+
61+
class User
62+
{
63+
public static function loadValidationMetadata(ClassMetadata $metadata)
64+
{
65+
$metadata->addPropertyConstraint('country', new Assert\Country());
66+
}
67+
}
68+
5369
Options
5470
-------
5571

reference/constraints/Date.rst

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ Basic Usage
3131
.. code-block:: php-annotations
3232
3333
// src/Acme/BlogBundle/Entity/Author.php
34+
namespace Acme\BlogBundle\Entity;
35+
3436
use Symfony\Component\Validator\Constraints as Assert;
3537
3638
class Author
@@ -50,6 +52,22 @@ Basic Usage
5052
</property>
5153
</class>
5254
55+
.. code-block:: php
56+
57+
// src/Acme/BlogBundle/Entity/Author.php
58+
namespace Acme\BlogBundle\Entity;
59+
60+
use Symfony\Component\Validator\Mapping\ClassMetadata;
61+
use Symfony\Component\Validator\Constraints as Assert;
62+
63+
class Author
64+
{
65+
public static function loadValidatorMetadata(ClassMetadata $metadata)
66+
{
67+
$metadata->addPropertyConstraint('birthday', new Assert\Date());
68+
}
69+
}
70+
5371
Options
5472
-------
5573

reference/constraints/DateTime.rst

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,22 @@ Basic Usage
5252
</property>
5353
</class>
5454
55+
.. code-block:: php
56+
57+
// src/Acme/BlogBundle/Entity/Author.php
58+
namespace Acme\BlogBundle\Entity;
59+
60+
use Symfony\Component\Validator\Mapping\ClassMetadata;
61+
use Symfony\Component\Validator\Constraints as Assert;
62+
63+
class Author
64+
{
65+
public static function loadValidatorMetadata(ClassMetadata $metadata)
66+
{
67+
$metadata->addPropertyConstraint('createdAt', new Assert\DataTime());
68+
}
69+
}
70+
5571
Options
5672
-------
5773

reference/constraints/Email.rst

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,25 @@ Basic Usage
6565
protected $email;
6666
}
6767
68+
.. code-block:: php
69+
70+
// src/Acme/BlogBundle/Entity/Author.php
71+
namespace Acme\BlogBundle\Entity;
72+
73+
use Symfony\Component\Validator\Mapping\ClassMetadata;
74+
use Symfony\Component\Validator\Constraints as Assert;
75+
76+
class Author
77+
{
78+
public static function loadValidatorMetadata(ClassMetadata $metadata)
79+
{
80+
$metadata->addPropertyConstraint('email', new Assert\Email(array(
81+
'message' => 'The email "{{ value }}" is not a valid email.',
82+
'check' => true,
83+
)));
84+
}
85+
}
86+
6887
Options
6988
-------
7089

@@ -83,4 +102,4 @@ checkMX
83102
If true, then the `checkdnsrr`_ PHP function will be used to check the validity
84103
of the MX record of the host of the given email.
85104

86-
.. _`checkdnsrr`: http://www.php.net/manual/en/function.checkdnsrr.php
105+
.. _`checkdnsrr`: http://www.php.net/manual/en/function.checkdnsrr.php

0 commit comments

Comments
 (0)