@@ -4,8 +4,8 @@ GreaterThan
44.. versionadded :: 2.3
55 The ``GreaterThan `` constraint was introduced in Symfony 2.3.
66
7- Validates that a value is greater than another value, defined in the options.
8- To force that a value is greater than or equal to another value, see
7+ Validates that a value is greater than another value, defined in the options. To
8+ force that a value is greater than or equal to another value, see
99:doc: `/reference/constraints/GreaterThanOrEqual `. To force a value is less
1010than another value, see :doc: `/reference/constraints/LessThan `.
1111
@@ -14,7 +14,6 @@ than another value, see :doc:`/reference/constraints/LessThan`.
1414+----------------+---------------------------------------------------------------------------+
1515| Options | - `value `_ |
1616| | - `message `_ |
17- | | - `payload `_ |
1817+----------------+---------------------------------------------------------------------------+
1918| Class | :class: `Symfony\\ Component\\ Validator\\ Constraints\\ GreaterThan ` |
2019+----------------+---------------------------------------------------------------------------+
@@ -24,11 +23,20 @@ than another value, see :doc:`/reference/constraints/LessThan`.
2423Basic Usage
2524-----------
2625
27- If you want to ensure that the ``age `` of a ``Person `` class is greater
28- than ``18 ``, you could do the following:
26+ If you want to ensure that the ``age `` of a ``Person `` class is greater than
27+ ``18 ``, you could do the following:
2928
3029.. configuration-block ::
3130
31+ .. code-block :: yaml
32+
33+ # src/SocialBundle/Resources/config/validation.yml
34+ Acme\SocialBundle\Entity\Person :
35+ properties :
36+ age :
37+ - GreaterThan :
38+ value : 18
39+
3240 .. code-block :: php-annotations
3341
3442 // src/Acme/SocialBundle/Entity/Person.php
@@ -46,31 +54,16 @@ than ``18``, you could do the following:
4654 protected $age;
4755 }
4856
49- .. code-block :: yaml
50-
51- # src/Acme/SocialBundle/Resources/config/validation.yml
52- Acme\SocialBundle\Entity\Person :
53- properties :
54- age :
55- - GreaterThan :
56- value : 18
57-
5857 .. code-block :: xml
5958
6059 <!-- src/Acme/SocialBundle/Resources/config/validation.xml -->
61- <?xml version =" 1.0" encoding =" UTF-8" ?>
62- <constraint-mapping xmlns =" http://symfony.com/schema/dic/constraint-mapping"
63- xmlns : xsi =" http://www.w3.org/2001/XMLSchema-instance"
64- xsi : schemaLocation =" http://symfony.com/schema/dic/constraint-mapping http://symfony.com/schema/dic/constraint-mapping/constraint-mapping-1.0.xsd" >
65-
66- <class name =" Acme\SocialBundle\Entity\Person" >
67- <property name =" age" >
68- <constraint name =" GreaterThan" >
69- <option name =" value" >18</option >
70- </constraint >
71- </property >
72- </class >
73- </constraint-mapping >
60+ <class name =" Acme\SocialBundle\Entity\Person" >
61+ <property name =" age" >
62+ <constraint name =" GreaterThan" >
63+ <option name =" value" >18</option >
64+ </constraint >
65+ </property >
66+ </class >
7467
7568 .. code-block :: php
7669
@@ -90,6 +83,170 @@ than ``18``, you could do the following:
9083 }
9184 }
9285
86+ Comparing Dates
87+ ---------------
88+
89+ This constraint can be used to compare ``DateTime `` objects against any date
90+ string `accepted by the DateTime constructor `_. For example, you could check
91+ that a date must at least be the next day:
92+
93+ .. configuration-block ::
94+
95+ .. code-block :: yaml
96+
97+ # src/OrderBundle/Resources/config/validation.yml
98+ Acme\OrderBundle\Entity\Order :
99+ properties :
100+ deliveryDate :
101+ - GreaterThan : today
102+
103+ .. code-block :: php-annotations
104+
105+ // src/Acme/SocialBundle/Entity/Order.php
106+ namespace Acme\OrderBundle\Entity;
107+
108+ use Symfony\Component\Validator\Constraints as Assert;
109+
110+ class Order
111+ {
112+ /**
113+ * @Assert\GreaterThan("today")
114+ */
115+ protected $deliveryDate;
116+ }
117+
118+ .. code-block :: xml
119+
120+ <!-- src/Acme/OrderBundle/Resources/config/validation.xml -->
121+ <class name =" Acme\OrderBundle\Entity\Order" >
122+ <property name =" deliveryDate" >
123+ <constraint name =" GreaterThan" >today</constraint >
124+ </property >
125+ </class >
126+
127+ .. code-block :: php
128+
129+ // src/Acme/OrderBundle/Entity/Order.php
130+ namespace Acme\OrderBundle\Entity;
131+
132+ use Symfony\Component\Validator\Mapping\ClassMetadata;
133+ use Symfony\Component\Validator\Constraints as Assert;
134+
135+ class Order
136+ {
137+ public static function loadValidatorMetadata(ClassMetadata $metadata)
138+ {
139+ $metadata->addPropertyConstraint('deliveryDate', new Assert\GreaterThan('today'));
140+ }
141+ }
142+
143+ Be aware that PHP will use the server's configured timezone to interpret these
144+ dates. If you want to fix the timezone, append it to the date string:
145+
146+ .. configuration-block ::
147+
148+ .. code-block :: yaml
149+
150+ # src/OrderBundle/Resources/config/validation.yml
151+ Acme\OrderBundle\Entity\Order :
152+ properties :
153+ deliveryDate :
154+ - GreaterThan : today UTC
155+
156+ .. code-block :: php-annotations
157+
158+ // src/Acme/SocialBundle/Entity/Order.php
159+ namespace Acme\OrderBundle\Entity;
160+
161+ use Symfony\Component\Validator\Constraints as Assert;
162+
163+ class Order
164+ {
165+ /**
166+ * @Assert\GreaterThan("today UTC")
167+ */
168+ protected $deliveryDate;
169+ }
170+
171+ .. code-block :: xml
172+
173+ <!-- src/Acme/OrderBundle/Resources/config/validation.xml -->
174+ <class name =" Acme\OrderBundle\Entity\Order" >
175+ <property name =" deliveryDate" >
176+ <constraint name =" GreaterThan" >today UTC</constraint >
177+ </property >
178+ </class >
179+
180+ .. code-block :: php
181+
182+ // src/Acme/OrderBundle/Entity/Order.php
183+ namespace Acme\OrderBundle\Entity;
184+
185+ use Symfony\Component\Validator\Mapping\ClassMetadata;
186+ use Symfony\Component\Validator\Constraints as Assert;
187+
188+ class Order
189+ {
190+ public static function loadValidatorMetadata(ClassMetadata $metadata)
191+ {
192+ $metadata->addPropertyConstraint('deliveryDate', new Assert\GreaterThan('today UTC'));
193+ }
194+ }
195+
196+ The ``DateTime `` class also accepts relative dates or times. For example, you
197+ can check that the above delivery date starts at least five hours after the
198+ current time:
199+
200+ .. configuration-block ::
201+
202+ .. code-block :: yaml
203+
204+ # src/OrderBundle/Resources/config/validation.yml
205+ Acme\OrderBundle\Entity\Order :
206+ properties :
207+ deliveryDate :
208+ - GreaterThan : +5 hours
209+
210+ .. code-block :: php-annotations
211+
212+ // src/Acme/SocialBundle/Entity/Order.php
213+ namespace Acme\OrderBundle\Entity;
214+
215+ use Symfony\Component\Validator\Constraints as Assert;
216+
217+ class Order
218+ {
219+ /**
220+ * @Assert\GreaterThan("+5 hours")
221+ */
222+ protected $deliveryDate;
223+ }
224+
225+ .. code-block :: xml
226+
227+ <!-- src/Acme/OrderBundle/Resources/config/validation.xml -->
228+ <class name =" Acme\OrderBundle\Entity\Order" >
229+ <property name =" deliveryDate" >
230+ <constraint name =" GreaterThan" >+5 hours</constraint >
231+ </property >
232+ </class >
233+
234+ .. code-block :: php
235+
236+ // src/Acme/OrderBundle/Entity/Order.php
237+ namespace Acme\OrderBundle\Entity;
238+
239+ use Symfony\Component\Validator\Mapping\ClassMetadata;
240+ use Symfony\Component\Validator\Constraints as Assert;
241+
242+ class Order
243+ {
244+ public static function loadValidatorMetadata(ClassMetadata $metadata)
245+ {
246+ $metadata->addPropertyConstraint('deliveryDate', new Assert\GreaterThan('+5 hours'));
247+ }
248+ }
249+
93250 Options
94251-------
95252
@@ -100,7 +257,7 @@ message
100257
101258**type **: ``string `` **default **: ``This value should be greater than {{ compared_value }}. ``
102259
103- This is the message that will be shown if the value is not greater than
104- the comparison value.
260+ This is the message that will be shown if the value is not greater than the
261+ comparison value.
105262
106- .. include :: /reference/constraints/_payload-option.rst.inc
263+ .. _ `accepted by the DateTime constructor` : http://www.php.net/manual/en/datetime.formats.php
0 commit comments