-
-
Notifications
You must be signed in to change notification settings - Fork 5.2k
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
Added information about the new date handling in the comparison constraints and Range #5384
Closed
Closed
Changes from 2 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
7aba65f
Added information about the new date handling in the comparison const…
webmozart 5a6533b
Finished the documentation of the new data comparison validators
javiereguiluz e4b776e
Fixed the issues reported by @xabbuh
javiereguiluz a9ba528
Reordered the code blocks to show Annotations, YAML, XML and PHP
javiereguiluz 918df0a
Show annotations first
javiereguiluz c5d0c8f
Added the "payload" option back
javiereguiluz File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,8 +4,8 @@ GreaterThan | |
.. versionadded:: 2.3 | ||
The ``GreaterThan`` constraint was introduced in Symfony 2.3. | ||
|
||
Validates that a value is greater than another value, defined in the options. | ||
To force that a value is greater than or equal to another value, see | ||
Validates that a value is greater than another value, defined in the options. To | ||
force that a value is greater than or equal to another value, see | ||
:doc:`/reference/constraints/GreaterThanOrEqual`. To force a value is less | ||
than another value, see :doc:`/reference/constraints/LessThan`. | ||
|
||
|
@@ -14,7 +14,6 @@ than another value, see :doc:`/reference/constraints/LessThan`. | |
+----------------+---------------------------------------------------------------------------+ | ||
| Options | - `value`_ | | ||
| | - `message`_ | | ||
| | - `payload`_ | | ||
+----------------+---------------------------------------------------------------------------+ | ||
| Class | :class:`Symfony\\Component\\Validator\\Constraints\\GreaterThan` | | ||
+----------------+---------------------------------------------------------------------------+ | ||
|
@@ -24,11 +23,20 @@ than another value, see :doc:`/reference/constraints/LessThan`. | |
Basic Usage | ||
----------- | ||
|
||
If you want to ensure that the ``age`` of a ``Person`` class is greater | ||
than ``18``, you could do the following: | ||
If you want to ensure that the ``age`` of a ``Person`` class is greater than | ||
``18``, you could do the following: | ||
|
||
.. configuration-block:: | ||
|
||
.. code-block:: yaml | ||
|
||
# src/SocialBundle/Resources/config/validation.yml | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
Acme\SocialBundle\Entity\Person: | ||
properties: | ||
age: | ||
- GreaterThan: | ||
value: 18 | ||
|
||
.. code-block:: php-annotations | ||
|
||
// src/Acme/SocialBundle/Entity/Person.php | ||
|
@@ -46,15 +54,6 @@ than ``18``, you could do the following: | |
protected $age; | ||
} | ||
|
||
.. code-block:: yaml | ||
|
||
# src/Acme/SocialBundle/Resources/config/validation.yml | ||
Acme\SocialBundle\Entity\Person: | ||
properties: | ||
age: | ||
- GreaterThan: | ||
value: 18 | ||
|
||
.. code-block:: xml | ||
|
||
<!-- src/Acme/SocialBundle/Resources/config/validation.xml --> | ||
|
@@ -90,6 +89,191 @@ than ``18``, you could do the following: | |
} | ||
} | ||
|
||
Comparing Dates | ||
--------------- | ||
|
||
.. versionadded:: 2.6 | ||
The feature to compare dates was added in Symfony 2.6. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. was introduced |
||
|
||
This constraint can be used to compare ``DateTime`` objects against any date | ||
string `accepted by the DateTime constructor`_. For example, you could check | ||
that a date must at least be the next day: | ||
|
||
.. configuration-block:: | ||
|
||
.. code-block:: yaml | ||
|
||
# src/Acme/OrderBundle/Resources/config/validation.yml | ||
Acme\OrderBundle\Entity\Order: | ||
properties: | ||
deliveryDate: | ||
- GreaterThan: today | ||
|
||
.. code-block:: php-annotations | ||
|
||
// src/Acme/OrderBundle/Entity/Order.php | ||
namespace Acme\OrderBundle\Entity; | ||
|
||
use Symfony\Component\Validator\Constraints as Assert; | ||
|
||
class Order | ||
{ | ||
/** | ||
* @Assert\GreaterThan("today") | ||
*/ | ||
protected $deliveryDate; | ||
} | ||
|
||
.. code-block:: xml | ||
|
||
<!-- src/Acme/OrderBundle/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\OrderBundle\Entity\Order"> | ||
<property name="deliveryDate"> | ||
<constraint name="GreaterThan">today</constraint> | ||
</property> | ||
</class> | ||
</constraint-mapping> | ||
|
||
.. code-block:: php | ||
|
||
// src/Acme/OrderBundle/Entity/Order.php | ||
namespace Acme\OrderBundle\Entity; | ||
|
||
use Symfony\Component\Validator\Mapping\ClassMetadata; | ||
use Symfony\Component\Validator\Constraints as Assert; | ||
|
||
class Order | ||
{ | ||
public static function loadValidatorMetadata(ClassMetadata $metadata) | ||
{ | ||
$metadata->addPropertyConstraint('deliveryDate', new Assert\GreaterThan('today')); | ||
} | ||
} | ||
|
||
Be aware that PHP will use the server's configured timezone to interpret these | ||
dates. If you want to fix the timezone, append it to the date string: | ||
|
||
.. configuration-block:: | ||
|
||
.. code-block:: yaml | ||
|
||
# src/Acme/OrderBundle/Resources/config/validation.yml | ||
Acme\OrderBundle\Entity\Order: | ||
properties: | ||
deliveryDate: | ||
- GreaterThan: today UTC | ||
|
||
.. code-block:: php-annotations | ||
|
||
// src/Acme/OrderBundle/Entity/Order.php | ||
namespace Acme\OrderBundle\Entity; | ||
|
||
use Symfony\Component\Validator\Constraints as Assert; | ||
|
||
class Order | ||
{ | ||
/** | ||
* @Assert\GreaterThan("today UTC") | ||
*/ | ||
protected $deliveryDate; | ||
} | ||
|
||
.. code-block:: xml | ||
|
||
<!-- src/Acme/OrderBundle/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\OrderBundle\Entity\Order"> | ||
<property name="deliveryDate"> | ||
<constraint name="GreaterThan">today UTC</constraint> | ||
</property> | ||
</class> | ||
</constraint-mapping> | ||
|
||
.. code-block:: php | ||
|
||
// src/Acme/OrderBundle/Entity/Order.php | ||
namespace Acme\OrderBundle\Entity; | ||
|
||
use Symfony\Component\Validator\Mapping\ClassMetadata; | ||
use Symfony\Component\Validator\Constraints as Assert; | ||
|
||
class Order | ||
{ | ||
public static function loadValidatorMetadata(ClassMetadata $metadata) | ||
{ | ||
$metadata->addPropertyConstraint('deliveryDate', new Assert\GreaterThan('today UTC')); | ||
} | ||
} | ||
|
||
The ``DateTime`` class also accepts relative dates or times. For example, you | ||
can check that the above delivery date starts at least five hours after the | ||
current time: | ||
|
||
.. configuration-block:: | ||
|
||
.. code-block:: yaml | ||
|
||
# src/Acme/OrderBundle/Resources/config/validation.yml | ||
Acme\OrderBundle\Entity\Order: | ||
properties: | ||
deliveryDate: | ||
- GreaterThan: +5 hours | ||
|
||
.. code-block:: php-annotations | ||
|
||
// src/Acme/OrderBundle/Entity/Order.php | ||
namespace Acme\OrderBundle\Entity; | ||
|
||
use Symfony\Component\Validator\Constraints as Assert; | ||
|
||
class Order | ||
{ | ||
/** | ||
* @Assert\GreaterThan("+5 hours") | ||
*/ | ||
protected $deliveryDate; | ||
} | ||
|
||
.. code-block:: xml | ||
|
||
<!-- src/Acme/OrderBundle/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\OrderBundle\Entity\Order"> | ||
<property name="deliveryDate"> | ||
<constraint name="GreaterThan">+5 hours</constraint> | ||
</property> | ||
</class> | ||
</constraint-mapping> | ||
|
||
.. code-block:: php | ||
|
||
// src/Acme/OrderBundle/Entity/Order.php | ||
namespace Acme\OrderBundle\Entity; | ||
|
||
use Symfony\Component\Validator\Mapping\ClassMetadata; | ||
use Symfony\Component\Validator\Constraints as Assert; | ||
|
||
class Order | ||
{ | ||
public static function loadValidatorMetadata(ClassMetadata $metadata) | ||
{ | ||
$metadata->addPropertyConstraint('deliveryDate', new Assert\GreaterThan('+5 hours')); | ||
} | ||
} | ||
|
||
Options | ||
------- | ||
|
||
|
@@ -100,7 +284,7 @@ message | |
|
||
**type**: ``string`` **default**: ``This value should be greater than {{ compared_value }}.`` | ||
|
||
This is the message that will be shown if the value is not greater than | ||
the comparison value. | ||
This is the message that will be shown if the value is not greater than the | ||
comparison value. | ||
|
||
.. include:: /reference/constraints/_payload-option.rst.inc | ||
.. _`accepted by the DateTime constructor`: http://www.php.net/manual/en/datetime.formats.php |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
payload
option should be kept.