Skip to content

Commit 72a6d59

Browse files
committed
Added PHP config format
1 parent d9c5950 commit 72a6d59

23 files changed

+329
-16
lines changed

reference/constraints/All.rst

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,14 +58,33 @@ 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+
use Symfony\Component\Validator\Mapping\ClassMetadata;
73+
use Symfony\Component\Validator\Constraints as Assert;
74+
75+
class User
76+
{
77+
public static function loadValidatorMetadata(ClassMetadata $metadata)
78+
{
79+
$metadata->addPropertyConstraint('favoriteColors', new Assert\All(array(
80+
'constraints' => array(
81+
new Assert\NotBlank(),
82+
new Assert\MinLength(array('limit' => 5)),
83+
),
84+
)));
85+
}
86+
}
87+
6988
Now, each entry in the ``favoriteColors`` array will be validated to not
7089
be blank and to be at least 5 characters long.
7190

reference/constraints/Blank.rst

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,20 @@ of an ``Author`` class were blank, you could do the following:
5252
</property>
5353
</class>
5454
55+
.. code-block:: php
56+
57+
// src/Acme/BlogBundle/Entity/Author.php
58+
use Symfony\Component\Validator\Mapping\ClassMetadata;
59+
use Symfony\Component\Validator\Constraints as Assert;
60+
61+
class Author
62+
{
63+
public static function loadValidatorMetadata(ClassMetadata $metadata)
64+
{
65+
$metadata->addPropertyConstraint('firstName', new Assetc\Blank());
66+
}
67+
}
68+
5569
Options
5670
-------
5771

reference/constraints/Callback.rst

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,22 @@ Setup
6363
</constraint>
6464
</class>
6565
66+
.. code-block:: php
67+
68+
// src/Acme/BlogBundle/Entity/Author.php
69+
use Symfony\Component\Validator\Mapping\ClassMetadata;
70+
use Symfony\Component\Validator\Constraints as Assert;
71+
72+
class Author
73+
{
74+
public static function loadValidatorMetadata(ClassMetadata $metadata)
75+
{
76+
$metadata->addConstraint(new Assert\Callback(array(
77+
'methods' => array('isAuthorValid'),
78+
)));
79+
}
80+
}
81+
6682
The Callback Method
6783
-------------------
6884

reference/constraints/Choice.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,15 +78,15 @@ If your valid choice list is simple, you can pass them in directly via the
7878
7979
// src/Acme/BlogBundle/EntityAuthor.php
8080
use Symfony\Component\Validator\Mapping\ClassMetadata;
81-
use Symfony\Component\Validator\Constraints\Choice;
81+
use Symfony\Component\Validator\Constraints as Assert;
8282
8383
class Author
8484
{
8585
protected $gender;
8686
8787
public static function loadValidatorMetadata(ClassMetadata $metadata)
8888
{
89-
$metadata->addPropertyConstraint('gender', new Choice(array(
89+
$metadata->addPropertyConstraint('gender', new Assert\Choice(array(
9090
'choices' => array('male', 'female'),
9191
'message' => 'Choose a valid gender',
9292
)));

reference/constraints/Collection.rst

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -120,20 +120,18 @@ blank but is no longer than 100 characters in length, you would do the following
120120
121121
// src/Acme/BlogBundle/Entity/Author.php
122122
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;
123+
use Symfony\Component\Validator\Constraints as Assert;
126124
127125
class Author
128126
{
129127
private $options = array();
130128
131129
public static function loadValidatorMetadata(ClassMetadata $metadata)
132130
{
133-
$metadata->addPropertyConstraint('profileData', new Collection(array(
131+
$metadata->addPropertyConstraint('profileData', new Assert\Collection(array(
134132
'fields' => array(
135-
'personal_email' => new Email(),
136-
'lastName' => array(new NotBlank(), new MaxLength(100)),
133+
'personal_email' => new Assert\Email(),
134+
'lastName' => array(new Assert\NotBlank(), new Assert\MaxLength(100)),
137135
),
138136
'allowMissingFields' => true,
139137
)));

reference/constraints/Country.rst

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,20 @@ Basic Usage
5050
</property>
5151
</class>
5252
53+
.. code-block:: php
54+
55+
// src/Acme/UserBundle/Entity/User.php
56+
use Symfony\Component\Validator\Mapping\ClassMetadata;
57+
use Symfony\Component\Validator\Constraints as Assert;
58+
59+
class User
60+
{
61+
public static function loadValidationMetadata(ClassMetadata $metadata)
62+
{
63+
$metadata->addPropertyConstraint('country', new Assert\Country());
64+
}
65+
}
66+
5367
Options
5468
-------
5569

reference/constraints/Date.rst

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,20 @@ Basic Usage
5050
</property>
5151
</class>
5252
53+
.. code-block:: php
54+
55+
// src/Acme/BlogBundle/Entity/Author.php
56+
use Symfony\Component\Validator\Mapping\ClassMetadata;
57+
use Symfony\Component\Validator\Constraints as Assert;
58+
59+
class Author
60+
{
61+
public static function loadValidatorMetadata(ClassMetadata $metadata)
62+
{
63+
$metadata->addPropertyConstraint('birthday', new Assert\Date());
64+
}
65+
}
66+
5367
Options
5468
-------
5569

reference/constraints/DateTime.rst

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

reference/constraints/Email.rst

Lines changed: 19 additions & 0 deletions
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

reference/constraints/False.rst

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,20 @@ method returns **false**:
7575
}
7676
}
7777
78+
.. code-block:: php
79+
80+
// src/Acme/BlogBundle/Entity/Author.php
81+
use Symfony\Component\Validator\Mapping\ClassMetadata;
82+
use Symfony\Component\Validator\Constraints as Assert;
83+
84+
class Author
85+
{
86+
public static function loadValidatorMetadata(ClassMetadata $metadata)
87+
{
88+
$metadata->addGetterConstraint('stateInvalid', new Assert\False());
89+
}
90+
}
91+
7892
.. caution::
7993

8094
When using YAML, be sure to surround ``False`` with quotes (``"False"``)

0 commit comments

Comments
 (0)