Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 5c064e7

Browse files
committed
feature #5384 Added information about the new date handling in the comparison constraints and Range (webmozart, javiereguiluz)
This PR was submitted for the 2.7 branch but it was merged into the 2.6 branch instead (closes #5384). Discussion ---------- Added information about the new date handling in the comparison constraints and Range | Q | A | ------------- | --- | Doc fix? | no | New docs? | yes (symfony/symfony#11673) | Applies to | 2.6+ | Fixed tickets | - This PR finishes #4143 Commits ------- b6c1a93 Added the "payload" option back 7ef2e6a Show annotations first e3efbbf Reordered the code blocks to show Annotations, YAML, XML and PHP 39f46e1 Fixed the issues reported by @xabbuh 7003445 Finished the documentation of the new data comparison validators 1fa69fe Added information about the new date handling in the comparison constraints and Range
2 parents 2f64d2b + b6c1a93 commit 5c064e7

File tree

5 files changed

+982
-20
lines changed

5 files changed

+982
-20
lines changed

reference/constraints/GreaterThan.rst

Lines changed: 193 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -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
1010
than another value, see :doc:`/reference/constraints/LessThan`.
1111

@@ -24,8 +24,8 @@ 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
28-
than ``18``, you could do the following:
27+
If you want to ensure that the ``age`` of a ``Person`` class is greater than
28+
``18``, you could do the following:
2929

3030
.. configuration-block::
3131

@@ -90,6 +90,191 @@ than ``18``, you could do the following:
9090
}
9191
}
9292
93+
Comparing Dates
94+
---------------
95+
96+
.. versionadded:: 2.6
97+
The feature to compare dates was introduced in Symfony 2.6.
98+
99+
This constraint can be used to compare ``DateTime`` objects against any date
100+
string `accepted by the DateTime constructor`_. For example, you could check
101+
that a date must at least be the next day:
102+
103+
.. configuration-block::
104+
105+
.. code-block:: php-annotations
106+
107+
// src/Acme/OrderBundle/Entity/Order.php
108+
namespace Acme\OrderBundle\Entity;
109+
110+
use Symfony\Component\Validator\Constraints as Assert;
111+
112+
class Order
113+
{
114+
/**
115+
* @Assert\GreaterThan("today")
116+
*/
117+
protected $deliveryDate;
118+
}
119+
120+
.. code-block:: yaml
121+
122+
# src/Acme/OrderBundle/Resources/config/validation.yml
123+
Acme\OrderBundle\Entity\Order:
124+
properties:
125+
deliveryDate:
126+
- GreaterThan: today
127+
128+
.. code-block:: xml
129+
130+
<!-- src/Acme/OrderBundle/Resources/config/validation.xml -->
131+
<?xml version="1.0" encoding="UTF-8" ?>
132+
<constraint-mapping xmlns="http://symfony.com/schema/dic/constraint-mapping"
133+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
134+
xsi:schemaLocation="http://symfony.com/schema/dic/constraint-mapping http://symfony.com/schema/dic/constraint-mapping/constraint-mapping-1.0.xsd">
135+
136+
<class name="Acme\OrderBundle\Entity\Order">
137+
<property name="deliveryDate">
138+
<constraint name="GreaterThan">today</constraint>
139+
</property>
140+
</class>
141+
</constraint-mapping>
142+
143+
.. code-block:: php
144+
145+
// src/Acme/OrderBundle/Entity/Order.php
146+
namespace Acme\OrderBundle\Entity;
147+
148+
use Symfony\Component\Validator\Mapping\ClassMetadata;
149+
use Symfony\Component\Validator\Constraints as Assert;
150+
151+
class Order
152+
{
153+
public static function loadValidatorMetadata(ClassMetadata $metadata)
154+
{
155+
$metadata->addPropertyConstraint('deliveryDate', new Assert\GreaterThan('today'));
156+
}
157+
}
158+
159+
Be aware that PHP will use the server's configured timezone to interpret these
160+
dates. If you want to fix the timezone, append it to the date string:
161+
162+
.. configuration-block::
163+
164+
.. code-block:: php-annotations
165+
166+
// src/Acme/OrderBundle/Entity/Order.php
167+
namespace Acme\OrderBundle\Entity;
168+
169+
use Symfony\Component\Validator\Constraints as Assert;
170+
171+
class Order
172+
{
173+
/**
174+
* @Assert\GreaterThan("today UTC")
175+
*/
176+
protected $deliveryDate;
177+
}
178+
179+
.. code-block:: yaml
180+
181+
# src/Acme/OrderBundle/Resources/config/validation.yml
182+
Acme\OrderBundle\Entity\Order:
183+
properties:
184+
deliveryDate:
185+
- GreaterThan: today UTC
186+
187+
.. code-block:: xml
188+
189+
<!-- src/Acme/OrderBundle/Resources/config/validation.xml -->
190+
<?xml version="1.0" encoding="UTF-8" ?>
191+
<constraint-mapping xmlns="http://symfony.com/schema/dic/constraint-mapping"
192+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
193+
xsi:schemaLocation="http://symfony.com/schema/dic/constraint-mapping http://symfony.com/schema/dic/constraint-mapping/constraint-mapping-1.0.xsd">
194+
195+
<class name="Acme\OrderBundle\Entity\Order">
196+
<property name="deliveryDate">
197+
<constraint name="GreaterThan">today UTC</constraint>
198+
</property>
199+
</class>
200+
</constraint-mapping>
201+
202+
.. code-block:: php
203+
204+
// src/Acme/OrderBundle/Entity/Order.php
205+
namespace Acme\OrderBundle\Entity;
206+
207+
use Symfony\Component\Validator\Mapping\ClassMetadata;
208+
use Symfony\Component\Validator\Constraints as Assert;
209+
210+
class Order
211+
{
212+
public static function loadValidatorMetadata(ClassMetadata $metadata)
213+
{
214+
$metadata->addPropertyConstraint('deliveryDate', new Assert\GreaterThan('today UTC'));
215+
}
216+
}
217+
218+
The ``DateTime`` class also accepts relative dates or times. For example, you
219+
can check that the above delivery date starts at least five hours after the
220+
current time:
221+
222+
.. configuration-block::
223+
224+
.. code-block:: php-annotations
225+
226+
// src/Acme/OrderBundle/Entity/Order.php
227+
namespace Acme\OrderBundle\Entity;
228+
229+
use Symfony\Component\Validator\Constraints as Assert;
230+
231+
class Order
232+
{
233+
/**
234+
* @Assert\GreaterThan("+5 hours")
235+
*/
236+
protected $deliveryDate;
237+
}
238+
239+
.. code-block:: yaml
240+
241+
# src/Acme/OrderBundle/Resources/config/validation.yml
242+
Acme\OrderBundle\Entity\Order:
243+
properties:
244+
deliveryDate:
245+
- GreaterThan: +5 hours
246+
247+
.. code-block:: xml
248+
249+
<!-- src/Acme/OrderBundle/Resources/config/validation.xml -->
250+
<?xml version="1.0" encoding="UTF-8" ?>
251+
<constraint-mapping xmlns="http://symfony.com/schema/dic/constraint-mapping"
252+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
253+
xsi:schemaLocation="http://symfony.com/schema/dic/constraint-mapping http://symfony.com/schema/dic/constraint-mapping/constraint-mapping-1.0.xsd">
254+
255+
<class name="Acme\OrderBundle\Entity\Order">
256+
<property name="deliveryDate">
257+
<constraint name="GreaterThan">+5 hours</constraint>
258+
</property>
259+
</class>
260+
</constraint-mapping>
261+
262+
.. code-block:: php
263+
264+
// src/Acme/OrderBundle/Entity/Order.php
265+
namespace Acme\OrderBundle\Entity;
266+
267+
use Symfony\Component\Validator\Mapping\ClassMetadata;
268+
use Symfony\Component\Validator\Constraints as Assert;
269+
270+
class Order
271+
{
272+
public static function loadValidatorMetadata(ClassMetadata $metadata)
273+
{
274+
$metadata->addPropertyConstraint('deliveryDate', new Assert\GreaterThan('+5 hours'));
275+
}
276+
}
277+
93278
Options
94279
-------
95280

@@ -100,7 +285,9 @@ message
100285

101286
**type**: ``string`` **default**: ``This value should be greater than {{ compared_value }}.``
102287

103-
This is the message that will be shown if the value is not greater than
104-
the comparison value.
288+
This is the message that will be shown if the value is not greater than the
289+
comparison value.
105290

106291
.. include:: /reference/constraints/_payload-option.rst.inc
292+
293+
.. _`accepted by the DateTime constructor`: http://www.php.net/manual/en/datetime.formats.php

0 commit comments

Comments
 (0)