Skip to content

[WCM] [Validator] Added DateTimeRangeValidator #3019

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
156 changes: 156 additions & 0 deletions reference/constraints/DateTimeRange.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
Range
=====

Validates that a given DateTime is *between* some minimum and maximum DateTime.

+----------------+-----------------------------------------------------------------------------+
| Applies to | :ref:`property or method<validation-property-target>` |
+----------------+-----------------------------------------------------------------------------+
| Options | - `min`_ |
| | - `max`_ |
| | - `minMessage`_ |
| | - `maxMessage`_ |
| | - `invalidMessage`_ |
| | - `timezone` _ |
+----------------+-----------------------------------------------------------------------------+
| Class | :class:`Symfony\\Component\\Validator\\Constraints\\DateTimeRange` |
+----------------+-----------------------------------------------------------------------------+
| Validator | :class:`Symfony\\Component\\Validator\\Constraints\\DateTimeRangeValidator` |
+----------------+-----------------------------------------------------------------------------+

Basic Usage
-----------

To verify that the "date" field of a class is between October 1st 2013 and November 1st 2013,
you might add the following:

.. configuration-block::

.. code-block:: yaml

# src/Acme/EcomBundle/Resources/config/validation.yml
Acme\EcomBundle\Entity\Order:
properties:
date:
- DateTimeRange:
min: 2013-10-01T00:00:00Z
max: 2013-11-01T00:00:00Z
minMessage: "Your order date must be on or after October 1st, 2013"
maxMessage: "Your order date must be before or on November 1st, 2013"

.. code-block:: php-annotations

// src/Acme/EcomBundle/Entity/Order.php
namespace Acme\EcomBundle\Entity;

use Symfony\Component\Validator\Constraints as Assert;

class Order
{
/**
* @Assert\DateTimeRange(
* min = "2013-10-01T00:00:00Z",
* max = "2013-11-01T00:00:00Z",
* minMessage = "Your order date must be on or after October 1st, 2013",
* maxMessage = "Your order date must be before or on November 1st, 2013"
* )
*/
protected $date;
}

.. code-block:: xml

<!-- src/Acme/EcomBundle/Resources/config/validation.xml -->
<?xml version="1.0" encoding="UTF-8" ?>
<constraint-mapping xmlns="http://symfony.com/schema/dic/constraint-mapping"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://symfony.com/schema/dic/constraint-mapping http://symfony.com/schema/dic/constraint-mapping/constraint-mapping-1.0.xsd">

<class name="Acme\EcomBundle\Entity\Order">
<property name="date">
<constraint name="DateTimeRange">
<option name="min">2013-10-01T00:00:00Z</option>
<option name="max">2013-11-01T00:00:00Z</option>
<option name="minMessage">Your order date must be on or after October 1st, 2013</option>
<option name="maxMessage">Your order date must be before or on November 1st, 2013</option>
</constraint>
</property>
</class>
</constraint-mapping>

.. code-block:: php

// src/Acme/EcomBundle/Entity/Order.php
namespace Acme\EcomBundle\Entity;

use Symfony\Component\Validator\Mapping\ClassMetadata;
use Symfony\Component\Validator\Constraints as Assert;

class Order
{
public static function loadValidatorMetadata(ClassMetadata $metadata)
{
$metadata->addPropertyConstraint('height', new Assert\DateTimeRange(array(
'min' => new DateTime('2013-10-01T00:00:00Z'),
'max' => new DateTime('2013-11-01T00:00:00Z'),
'minMessage' => 'Your order date must be on or after October 1st, 2013',
'maxMessage' => 'Your order date must be before or on November 1st, 2013',
)));
}
}

Options
-------

min
~~~

**type**: ``DateTime|string`` [:ref:`default option<validation-default-option>`]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

there should be a space before the open angle bracket


This required option is the "min" value. Validation will fail if the given
value is **less** than this min value. You may specify this option as a ``DateTime``
or any ``string`` in a format supported by the ``DateTime`` constructor,
including relative formats.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I prefer minimum instead of min


max
~~~

**type**: ``DateTime|string`` [:ref:`default option<validation-default-option>`]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same here


This required option is the "max" value. Validation will fail if the given
value is **greater** than this max value. You may specify this option as a
``DateTime`` or any ``string`` in a `format supported by the ``DateTime`` constructor`_,
including relative formats.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

max -> maximum


timezone
~~~~~~~~

**type**: ``DateTimeZone|string`` **default**: ``UTC``

If the ``timezone`` value is specified, this timezone is used when transforming
``string`` values and `min`_ `max`_ options to ``DateTime``.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

min/max


minMessage
~~~~~~~~~~

**type**: ``string`` **default**: ``This value should be {{ limit }} or more.``

The message that will be shown if the underlying value is less than the `min`_
option.

maxMessage
~~~~~~~~~~

**type**: ``string`` **default**: ``This value should be {{ limit }} or less.``

The message that will be shown if the underlying value is more than the `max`_
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is more -> is greater/higher

option.

invalidMessage
~~~~~~~~~~~~~~

**type**: ``string`` **default**: ``This value is not a valid date.``

The message that will be shown if the underlying value is not a date.

.. _`format supported by the ``DateTime`` constructor`: http://www.php.net/manual/en/datetime.formats.php
1 change: 1 addition & 0 deletions reference/constraints/map.rst.inc
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ Date Constraints

* :doc:`Date </reference/constraints/Date>`
* :doc:`DateTime </reference/constraints/DateTime>`
* :doc:`DateTimeRange </reference/constraints/DateTimeRange>`
* :doc:`Time </reference/constraints/Time>`

Collection Constraints
Expand Down