Skip to content

Commit d3cde84

Browse files
alexmartxabbuh
alexmart
authored andcommitted
[Validator] Add shorter examples using the default option
1 parent e20d26b commit d3cde84

File tree

10 files changed

+165
-16
lines changed

10 files changed

+165
-16
lines changed

reference/constraints/Choice.rst

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,11 @@ If your valid choice list is simple, you can pass them in directly via the
4646
4747
class Author
4848
{
49+
/**
50+
* @Assert\Choice({"New York", "Berlin", "Tokyo"})
51+
*/
52+
protected $city;
53+
4954
/**
5055
* @Assert\Choice(choices = {"male", "female"}, message = "Choose a valid gender.")
5156
*/
@@ -57,6 +62,7 @@ If your valid choice list is simple, you can pass them in directly via the
5762
# src/AppBundle/Resources/config/validation.yml
5863
AppBundle\Entity\Author:
5964
properties:
65+
city: [New York, Berlin, Tokyo]
6066
gender:
6167
- Choice:
6268
choices: [male, female]
@@ -71,6 +77,13 @@ If your valid choice list is simple, you can pass them in directly via the
7177
xsi:schemaLocation="http://symfony.com/schema/dic/constraint-mapping http://symfony.com/schema/dic/constraint-mapping/constraint-mapping-1.0.xsd">
7278
7379
<class name="AppBundle\Entity\Author">
80+
<property name="city">
81+
<constraint name="Choice">
82+
<value>New York</value>
83+
<value>Berlin</value>
84+
<value>Tokyo</value>
85+
</constraint>
86+
</property>
7487
<property name="gender">
7588
<constraint name="Choice">
7689
<option name="choices">
@@ -97,6 +110,11 @@ If your valid choice list is simple, you can pass them in directly via the
97110
98111
public static function loadValidatorMetadata(ClassMetadata $metadata)
99112
{
113+
$metadata->addPropertyConstraint(
114+
'gender',
115+
new Assert\Choice(array('New York', 'Berlin', 'Tokyo'))
116+
);
117+
100118
$metadata->addPropertyConstraint('gender', new Assert\Choice(array(
101119
'choices' => array('male', 'female'),
102120
'message' => 'Choose a valid gender.',

reference/constraints/EqualTo.rst

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ To force that a value is *not* equal, see :doc:`/reference/constraints/NotEqualT
2828
Basic Usage
2929
-----------
3030

31-
If you want to ensure that the ``age`` of a ``Person`` class is equal to
32-
``20``, you could do the following:
31+
If you want to ensure that the ``firstName`` of a ``Person`` class is equal to ``Mary``
32+
and that the ``age`` is ``20``, you could do the following:
3333

3434
.. configuration-block::
3535

@@ -42,6 +42,11 @@ If you want to ensure that the ``age`` of a ``Person`` class is equal to
4242
4343
class Person
4444
{
45+
/**
46+
* @Assert\EqualTo("Mary")
47+
*/
48+
protected $firstName;
49+
4550
/**
4651
* @Assert\EqualTo(
4752
* value = 20
@@ -55,6 +60,8 @@ If you want to ensure that the ``age`` of a ``Person`` class is equal to
5560
# src/AppBundle/Resources/config/validation.yml
5661
AppBundle\Entity\Person:
5762
properties:
63+
firstName:
64+
- EqualTo: Mary
5865
age:
5966
- EqualTo:
6067
value: 20
@@ -68,6 +75,11 @@ If you want to ensure that the ``age`` of a ``Person`` class is equal to
6875
xsi:schemaLocation="http://symfony.com/schema/dic/constraint-mapping http://symfony.com/schema/dic/constraint-mapping/constraint-mapping-1.0.xsd">
6976
7077
<class name="AppBundle\Entity\Person">
78+
<property name="firstName">
79+
<constraint name="EqualTo">
80+
<value>Mary</value>
81+
</constraint>
82+
</property>
7183
<property name="age">
7284
<constraint name="EqualTo">
7385
<option name="value">20</option>
@@ -88,6 +100,8 @@ If you want to ensure that the ``age`` of a ``Person`` class is equal to
88100
{
89101
public static function loadValidatorMetadata(ClassMetadata $metadata)
90102
{
103+
$metadata->addPropertyConstraint('firstName', new Assert\EqualTo('Mary'));
104+
91105
$metadata->addPropertyConstraint('age', new Assert\EqualTo(array(
92106
'value' => 20,
93107
)));

reference/constraints/GreaterThan.rst

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,10 @@ than another value, see :doc:`/reference/constraints/LessThan`.
2424
Basic Usage
2525
-----------
2626

27-
If you want to ensure that the ``age`` of a ``Person`` class is greater than
28-
``18``, you could do the following:
27+
The following constraints ensure that:
28+
- the number of ``siblings`` of a ``Person`` is greater than ``5``
29+
- the ``age`` of a ``Person`` class is greater than ``18``
30+
2931

3032
.. configuration-block::
3133

@@ -38,6 +40,12 @@ If you want to ensure that the ``age`` of a ``Person`` class is greater than
3840
3941
class Person
4042
{
43+
44+
/**
45+
* @Assert\GreaterThan(5)
46+
*/
47+
protected $siblings;
48+
4149
/**
4250
* @Assert\GreaterThan(
4351
* value = 18
@@ -51,6 +59,8 @@ If you want to ensure that the ``age`` of a ``Person`` class is greater than
5159
# src/AppBundle/Resources/config/validation.yml
5260
AppBundle\Entity\Person:
5361
properties:
62+
siblings:
63+
- GreaterThan: 5
5464
age:
5565
- GreaterThan:
5666
value: 18
@@ -64,6 +74,11 @@ If you want to ensure that the ``age`` of a ``Person`` class is greater than
6474
xsi:schemaLocation="http://symfony.com/schema/dic/constraint-mapping http://symfony.com/schema/dic/constraint-mapping/constraint-mapping-1.0.xsd">
6575
6676
<class name="AppBundle\Entity\Person">
77+
<property name="siblings">
78+
<constraint name="GreaterThan">
79+
<value>5</value>
80+
</constraint>
81+
</property>
6782
<property name="age">
6883
<constraint name="GreaterThan">
6984
<option name="value">18</option>
@@ -84,6 +99,8 @@ If you want to ensure that the ``age`` of a ``Person`` class is greater than
8499
{
85100
public static function loadValidatorMetadata(ClassMetadata $metadata)
86101
{
102+
$metadata->addPropertyConstraint('siblings', new Assert\GreaterThan(5));
103+
87104
$metadata->addPropertyConstraint('age', new Assert\GreaterThan(array(
88105
'value' => 18,
89106
)));

reference/constraints/GreaterThanOrEqual.rst

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,9 @@ the options. To force that a value is greater than another value, see
2323
Basic Usage
2424
-----------
2525

26-
If you want to ensure that the ``age`` of a ``Person`` class is greater than
27-
or equal to ``18``, you could do the following:
26+
The following constraints ensure that:
27+
- the number of ``siblings`` of a ``Person`` is greater than or equal to ``5``
28+
- the ``age`` of a ``Person`` class is greater than or equal to ``18``
2829

2930
.. configuration-block::
3031

@@ -37,6 +38,11 @@ or equal to ``18``, you could do the following:
3738
3839
class Person
3940
{
41+
/**
42+
* @Assert\GreaterThanOrEqual(5)
43+
*/
44+
protected $siblings;
45+
4046
/**
4147
* @Assert\GreaterThanOrEqual(
4248
* value = 18
@@ -50,6 +56,8 @@ or equal to ``18``, you could do the following:
5056
# src/AppBundle/Resources/config/validation.yml
5157
AppBundle\Entity\Person:
5258
properties:
59+
siblings:
60+
- GreaterThanOrEqual: 5
5361
age:
5462
- GreaterThanOrEqual:
5563
value: 18
@@ -63,6 +71,11 @@ or equal to ``18``, you could do the following:
6371
xsi:schemaLocation="http://symfony.com/schema/dic/constraint-mapping http://symfony.com/schema/dic/constraint-mapping/constraint-mapping-1.0.xsd">
6472
6573
<class name="AppBundle\Entity\Person">
74+
<property name="siblings">
75+
<constraint name="GreaterThanOrEqual">
76+
<value>5</value>
77+
</constraint>
78+
</property>
6679
<property name="age">
6780
<constraint name="GreaterThanOrEqual">
6881
<option name="value">18</option>
@@ -83,6 +96,8 @@ or equal to ``18``, you could do the following:
8396
{
8497
public static function loadValidatorMetadata(ClassMetadata $metadata)
8598
{
99+
$metadata->addPropertyConstraint('siblings', new Assert\GreaterThanOrEqual(5));
100+
86101
$metadata->addPropertyConstraint('age', new Assert\GreaterThanOrEqual(array(
87102
'value' => 18,
88103
)));

reference/constraints/IdenticalTo.rst

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,9 @@ To force that a value is *not* identical, see
2929
Basic Usage
3030
-----------
3131

32-
If you want to ensure that the ``age`` of a ``Person`` class is equal to
33-
``20`` and an integer, you could do the following:
32+
The following constraints ensure that:
33+
- ``firstName`` of ``Person` class is equal to ``Mary`` *and* is a string
34+
- ``age`` is equal to``20`` *and* is of type integer
3435

3536
.. configuration-block::
3637

@@ -43,6 +44,11 @@ If you want to ensure that the ``age`` of a ``Person`` class is equal to
4344
4445
class Person
4546
{
47+
/**
48+
* @Assert\IdenticalTo("Mary")
49+
*/
50+
protected $firstName;
51+
4652
/**
4753
* @Assert\IdenticalTo(
4854
* value = 20

reference/constraints/LessThan.rst

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,9 @@ than another value, see :doc:`/reference/constraints/GreaterThan`.
2424
Basic Usage
2525
-----------
2626

27-
If you want to ensure that the ``age`` of a ``Person`` class is less than
28-
``80``, you could do the following:
27+
The following constraints ensure that:
28+
- the number of ``siblings`` of a ``Person`` is less than or equal to ``5``
29+
- ``age`` is less than``80``
2930

3031
.. configuration-block::
3132

@@ -38,6 +39,12 @@ If you want to ensure that the ``age`` of a ``Person`` class is less than
3839
3940
class Person
4041
{
42+
43+
/**
44+
* @Assert\LessThan(5)
45+
*/
46+
protected $siblings;
47+
4148
/**
4249
* @Assert\LessThan(
4350
* value = 80
@@ -51,6 +58,8 @@ If you want to ensure that the ``age`` of a ``Person`` class is less than
5158
# src/AppBundle/Resources/config/validation.yml
5259
AppBundle\Entity\Person:
5360
properties:
61+
siblings:
62+
- LessThan: 5
5463
age:
5564
- LessThan:
5665
value: 80
@@ -64,6 +73,11 @@ If you want to ensure that the ``age`` of a ``Person`` class is less than
6473
xsi:schemaLocation="http://symfony.com/schema/dic/constraint-mapping http://symfony.com/schema/dic/constraint-mapping/constraint-mapping-1.0.xsd">
6574
6675
<class name="AppBundle\Entity\Person">
76+
<property name="siblings">
77+
<constraint name="LessThan">
78+
<value>5</value>
79+
</constraint>
80+
</property>
6781
<property name="age">
6882
<constraint name="LessThan">
6983
<option name="value">80</option>
@@ -84,6 +98,8 @@ If you want to ensure that the ``age`` of a ``Person`` class is less than
8498
{
8599
public static function loadValidatorMetadata(ClassMetadata $metadata)
86100
{
101+
$metadata->addPropertyConstraint('siblings', new Assert\LessThan(5));
102+
87103
$metadata->addPropertyConstraint('age', new Assert\LessThan(array(
88104
'value' => 80,
89105
)));

reference/constraints/LessThanOrEqual.rst

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,9 @@ options. To force that a value is less than another value, see
2323
Basic Usage
2424
-----------
2525

26-
If you want to ensure that the ``age`` of a ``Person`` class is less than or
27-
equal to ``80``, you could do the following:
26+
The following constraints ensure that:
27+
- the number of ``siblings`` of a ``Person`` is less than or equal to ``5``
28+
- the ``age`` is less than or equal to ``80``
2829

2930
.. configuration-block::
3031

@@ -37,6 +38,11 @@ equal to ``80``, you could do the following:
3738
3839
class Person
3940
{
41+
/**
42+
* @Assert\LessThanOrEqual(5)
43+
*/
44+
protected $siblings;
45+
4046
/**
4147
* @Assert\LessThanOrEqual(
4248
* value = 80
@@ -50,6 +56,8 @@ equal to ``80``, you could do the following:
5056
# src/AppBundle/Resources/config/validation.yml
5157
AppBundle\Entity\Person:
5258
properties:
59+
siblings:
60+
- LessThanOrEqual: 5
5361
age:
5462
- LessThanOrEqual:
5563
value: 80
@@ -63,6 +71,11 @@ equal to ``80``, you could do the following:
6371
xsi:schemaLocation="http://symfony.com/schema/dic/constraint-mapping http://symfony.com/schema/dic/constraint-mapping/constraint-mapping-1.0.xsd">
6472
6573
<class name="AppBundle\Entity\Person">
74+
<property name="siblings">
75+
<constraint name="LessThanOrEqual">
76+
<value>5</value>
77+
</constraint>
78+
</property>
6679
<property name="age">
6780
<constraint name="LessThanOrEqual">
6881
<option name="value">80</option>
@@ -83,6 +96,8 @@ equal to ``80``, you could do the following:
8396
{
8497
public static function loadValidatorMetadata(ClassMetadata $metadata)
8598
{
99+
$metadata->addPropertyConstraint('siblings', new Assert\LessThanOrEqual(5));
100+
86101
$metadata->addPropertyConstraint('age', new Assert\LessThanOrEqual(array(
87102
'value' => 80,
88103
)));

reference/constraints/NotEqualTo.rst

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,10 @@ options. To force that a value is equal, see
2828

2929
Basic Usage
3030
-----------
31+
If you want to ensure that the ``firstName`` of a ``Person`` is not equal to
32+
``Mary`` and that the ``age`` of a ``Person`` class is not ``15``, you could do
33+
the following:
3134

32-
If you want to ensure that the ``age`` of a ``Person`` class is not equal
33-
to ``15``, you could do the following:
3435

3536
.. configuration-block::
3637

@@ -43,6 +44,11 @@ to ``15``, you could do the following:
4344
4445
class Person
4546
{
47+
/**
48+
* @Assert\NotEqualTo("Mary")
49+
*/
50+
protected $firstName;
51+
4652
/**
4753
* @Assert\NotEqualTo(
4854
* value = 15
@@ -56,6 +62,8 @@ to ``15``, you could do the following:
5662
# src/AppBundle/Resources/config/validation.yml
5763
AppBundle\Entity\Person:
5864
properties:
65+
firstName:
66+
- NotEqualTo: Mary
5967
age:
6068
- NotEqualTo:
6169
value: 15
@@ -69,6 +77,11 @@ to ``15``, you could do the following:
6977
xsi:schemaLocation="http://symfony.com/schema/dic/constraint-mapping http://symfony.com/schema/dic/constraint-mapping/constraint-mapping-1.0.xsd">
7078
7179
<class name="AppBundle\Entity\Person">
80+
<property name="firstName">
81+
<constraint name="NotEqualTo">
82+
<value>Mary</value>
83+
</constraint>
84+
</property>
7285
<property name="age">
7386
<constraint name="NotEqualTo">
7487
<option name="value">15</option>
@@ -89,6 +102,8 @@ to ``15``, you could do the following:
89102
{
90103
public static function loadValidatorMetadata(ClassMetadata $metadata)
91104
{
105+
$metadata->addPropertyConstraint('age', new Assert\NotEqualTo('Mary'));
106+
92107
$metadata->addPropertyConstraint('age', new Assert\NotEqualTo(array(
93108
'value' => 15,
94109
)));

0 commit comments

Comments
 (0)