Skip to content

Commit 3270cca

Browse files
committed
Document the DivisibleBy constraint
1 parent 302206a commit 3270cca

File tree

3 files changed

+131
-0
lines changed

3 files changed

+131
-0
lines changed

reference/constraints.rst

+1
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ Validation Constraints Reference
2929
constraints/GreaterThan
3030
constraints/GreaterThanOrEqual
3131
constraints/Range
32+
constraints/DivisibleBy
3233

3334
constraints/Date
3435
constraints/DateTime

reference/constraints/DivisibleBy.rst

+129
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
DivisibleBy
2+
===========
3+
4+
Validates that a value is divisible by another value, defined in the options.
5+
6+
+----------------+---------------------------------------------------------------------------+
7+
| Applies to | :ref:`property or method<validation-property-target>` |
8+
+----------------+---------------------------------------------------------------------------+
9+
| Options | - `value`_ |
10+
| | - `message`_ |
11+
| | - `payload`_ |
12+
| | - `propertyPath`_ |
13+
+----------------+---------------------------------------------------------------------------+
14+
| Class | :class:`Symfony\\Component\\Validator\\Constraints\\DivisibleBy` |
15+
+----------------+---------------------------------------------------------------------------+
16+
| Validator | :class:`Symfony\\Component\\Validator\\Constraints\\DivisibleByValidator` |
17+
+----------------+---------------------------------------------------------------------------+
18+
19+
Basic Usage
20+
-----------
21+
22+
The following constraints ensure that:
23+
24+
* the ``weight`` of the ``Item`` is provided in increments of ``0.25``
25+
* the ``quantity`` of the ``Item`` must be divisible by ``5``
26+
27+
.. configuration-block::
28+
29+
.. code-block:: php-annotations
30+
31+
// src/Entity/Item.php
32+
namespace App\Entity;
33+
34+
use Symfony\Component\Validator\Constraints as Assert;
35+
36+
class Item
37+
{
38+
39+
/**
40+
* @Assert\DivisibleBy(0.25)
41+
*/
42+
protected $weight;
43+
44+
/**
45+
* @Assert\DivisibleBy(
46+
* value = 5
47+
* )
48+
*/
49+
protected $quantity;
50+
}
51+
52+
.. code-block:: yaml
53+
54+
# config/validator/validation.yaml
55+
App\Entity\Item:
56+
properties:
57+
weight:
58+
- DivisibleBy: 0.25
59+
quantity:
60+
- DivisibleBy:
61+
value: 5
62+
63+
.. code-block:: xml
64+
65+
<!-- config/validator/validation.xml -->
66+
<?xml version="1.0" encoding="UTF-8" ?>
67+
<constraint-mapping xmlns="http://symfony.com/schema/dic/constraint-mapping"
68+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
69+
xsi:schemaLocation="http://symfony.com/schema/dic/constraint-mapping http://symfony.com/schema/dic/constraint-mapping/constraint-mapping-1.0.xsd">
70+
71+
<class name="App\Entity\Item">
72+
<property name="weight">
73+
<constraint name="DivisibleBy">
74+
<value>0.25</value>
75+
</constraint>
76+
</property>
77+
<property name="quantity">
78+
<constraint name="DivisibleBy">
79+
<option name="value">5</option>
80+
</constraint>
81+
</property>
82+
</class>
83+
</constraint-mapping>
84+
85+
.. code-block:: php
86+
87+
// src/Entity/Item.php
88+
namespace App\Entity;
89+
90+
use Symfony\Component\Validator\Mapping\ClassMetadata;
91+
use Symfony\Component\Validator\Constraints as Assert;
92+
93+
class Item
94+
{
95+
public static function loadValidatorMetadata(ClassMetadata $metadata)
96+
{
97+
$metadata->addPropertyConstraint('weight', new Assert\DivisibleBy(0.25));
98+
99+
$metadata->addPropertyConstraint('quantity', new Assert\DivisibleBy(array(
100+
'value' => 5,
101+
)));
102+
}
103+
}
104+
105+
Options
106+
-------
107+
108+
.. include:: /reference/constraints/_comparison-value-option.rst.inc
109+
110+
message
111+
~~~~~~~
112+
113+
**type**: ``string`` **default**: ``This value should be a multiple of {{ compared_value }}.``
114+
115+
This is the message that will be shown if the value is not divisible by the
116+
comparison value.
117+
118+
.. include:: /reference/constraints/_payload-option.rst.inc
119+
120+
propertyPath
121+
~~~~~~~~~~~~
122+
123+
**type**: ``string``
124+
125+
It defines the object property whose value is used to make the comparison.
126+
127+
For example, if you want to compare the ``$value`` property of some object
128+
with regard to the ``$increments`` property of the same object, use
129+
``propertyPath="increments"`` in the comparison constraint of ``$value``.

reference/constraints/map.rst.inc

+1
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ Comparison Constraints
3434
* :doc:`GreaterThan </reference/constraints/GreaterThan>`
3535
* :doc:`GreaterThanOrEqual </reference/constraints/GreaterThanOrEqual>`
3636
* :doc:`Range </reference/constraints/Range>`
37+
* :doc:`DivisibleBy </reference/constraints/DivisibleBy>`
3738

3839
Date Constraints
3940
~~~~~~~~~~~~~~~~

0 commit comments

Comments
 (0)