8000 [Validator] Add docs for number constraints · symfony/symfony-docs@8022292 · GitHub
[go: up one dir, main page]

Skip to content

Commit 8022292

Browse files
jschaedljaviereguiluz
authored andcommitted
[Validator] Add docs for number constraints
1 parent cd5c7eb commit 8022292

File tree

6 files changed

+438
-0
lines changed

6 files changed

+438
-0
lines changed

reference/constraints.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,11 @@ Validation Constraints Reference
3232
constraints/Range
3333
constraints/DivisibleBy
3434

35+
constraints/Positive
36+
constraints/PositiveOrZero
37+
constraints/Negative
38+
constraints/NegativeOrZero
39+
3540
constraints/Date
3641
constraints/DateTime
3742
constraints/Time

reference/constraints/Negative.rst

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
Negative
2+
========
3+
4+
.. versionadded:: 4.3
5+
6+
The ``Negative`` constraint was introduced in Symfony 4.3.
7+
8+
Validates that a value is a negative number. To force that a value is a negative
9+
number or equal to zero, see :doc:`/reference/constraints/NegativeOrZero`.
10+
To force a value is positive, see :doc:`/reference/constraints/Positive`.
11+
12+
========== ===================================================================
13+
Applies to :ref:`property or method <validation-property-target>`
14+
Options - `groups`_
15+
- `message`_
16+
- `payload`_
17+
Class :class:`Symfony\\Component\\Validator\\Constraints\\Negative`
18+
Validator :class:`Symfony\\Component\\Validator\\Constraints\\LesserThanValidator`
19+
========== ===================================================================
20+
21+
Basic Usage
22+
-----------
23+
24+
The following constraint ensure that:
25+
26+
* the ``withdraw`` of a bankaccount ``TransferItem`` is a negative number (lesser than zero)
27+
28+
.. configuration-block::
29+
30+
.. code-block:: php-annotations
31+
32+
// src/Entity/TransferItem.php
33+
namespace App\Entity;
34+
35+
use Symfony\Component\Validator\Constraints as Assert;
36+
37+
class TransferItem
38+
{
39+
/**
40+
* @Assert\Negative
41+
*/
42+
protected $withdraw;
43+
}
44+
45+
.. code-block:: yaml
46+
47+
# config/validator/validation.yaml
48+
App\Entity\TransferItem:
49+
properties:
50+
withdraw:
51+
- Negative
52+
53+
.. code-block:: xml
54+
55+
<!-- config/validator/validation.xml -->
56+
<?xml version="1.0" encoding="UTF-8" ?>
57+
<constraint-mapping xmlns="http://symfony.com/schema/dic/constraint-mapping"
58+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
59+
xsi:schemaLocation="http://symfony.com/schema/dic/constraint-mapping https://symfony.com/schema/dic/constraint-mapping/constraint-mapping-1.0.xsd">
60+
61+
<class name="App\Entity\TransferItem">
62+
<property name="withdraw">
63+
<constraint name="Negative"></constraint>
64+
</property>
65+
</class>
66+
</constraint-mapping>
67+
68+
.. code-block:: php
69+
70+
// src/Entity/TransferItem.php
71+
namespace App\Entity;
72+
73+
use Symfony\Component\Validator\Mapping\ClassMetadata;
74+
use Symfony\Component\Validator\Constraints as Assert;
75+
76+
class TransferItem
77+
{
78+
public static function loadValidatorMetadata(ClassMetadata $metadata)
79+
{
80+
$metadata->addPropertyConstraint('withdraw', new Assert\Negative());
81+
}
82+
}
83+
84+
Available Options
85+
-----------------
86+
87+
.. include:: /reference/constraints/_groups-option.rst.inc
88+
89+
message
90+
~~~~~~~
91+
92+
**type**: ``string`` **default**: ``This value should be negative.``
93+
94+
The default message supplied when the value is not less than zero.
95+
96+
You can use the following parameters in this message:
97+
98+
============================= ================================================
99+
Parameter Description
100+
============================= ================================================
101+
``{{ compared_value }}`` Always zero
102+
``{{ compared_value_type }}`` The expected value type
103+
``{{ value }}`` The current (invalid) value
104+
============================= ================================================
105+
106+
.. include:: /reference/constraints/_payload-option.rst.inc
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
NegativeOrZero
2+
==============
3+
4+
.. versionadded:: 4.3
5+
6+
The ``NegativeOrZero`` constraint was introduced in Symfony 4.3.
7+
8+
Validates that a value is a negative number or equal to zero. To force that a value
9+
is only a negative number, see :doc:`/reference/constraints/Negative`.
10+
To force a value is positive or equal to zero,
11+
see :doc:`/reference/constraints/PositiveOrZero`.
12+
13+
========== ===================================================================
14+
Applies to :ref:`property or method <validation-property-target>`
15+
Options - `groups`_
16+
- `message`_
17+
- `payload`_
18+
Class :class:`Symfony\\Component\\Validator\\Constraints\\NegativeOrZero`
19+
Validator :class:`Symfony\\Component\\Validator\\Constraints\\LesserThanOrEqualValidator`
20+
========== ===================================================================
21+
22+
Basic Usage
23+
-----------
24+
25+
The following constraint ensure that:
26+
27+
* the ``level`` of a ``UnderGroundGarage`` is a negative number or equal to zero
28+
29+
.. configuration-block::
30+
31+
.. code-block:: php-annotations
32+
33+
// src/Entity/TransferItem.php
34+
namespace App\Entity;
35+
36+
use Symfony\Component\Validator\Constraints as Assert;
37+
38+
class UnderGroundGarage
39+
{
40+
/**
41+
* @Assert\NegativeOrZero
42+
*/
43+
protected $level;
44+
}
45+
46+
.. code-block:: yaml
47+
48+
# config/validator/validation.yaml
49+
App\Entity\UnderGroundGarage:
50+
properties:
51+
level:
52+
- NegativeOrZero
53+
54+
.. code-block:: xml
55+
56+
<!-- config/validator/validation.xml -->
57+
<?xml version="1.0" encoding="UTF-8" ?>
58+
<constraint-mapping xmlns="http://symfony.com/schema/dic/constraint-mapping"
59+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
60+
xsi:schemaLocation="http://symfony.com/schema/dic/constraint-mapping https://symfony.com/schema/dic/constraint-mapping/constraint-mapping-1.0.xsd">
61+
62+
<class name="App\Entity\UnderGroundGarage">
63+
<property name="level">
64+
<constraint name="NegativeOrZero"></constraint>
65+
</property>
66+
</class>
67+
</constraint-mapping>
68+
69+
.. code-block:: php
70+
71+
// src/Entity/UnderGroundGarage.php
72+
namespace App\Entity;
73+
74+
use Symfony\Component\Validator\Mapping\ClassMetadata;
75+
use Symfony\Component\Validator\Constraints as Assert;
76+
77+
class UnderGroundGarage
78+
{
79+
public static function loadValidatorMetadata(ClassMetadata $metadata)
80+
{
81+
$metadata->addPropertyConstraint('level', new Assert\NegativeOrZero());
82+
}
83+
}
84+
85+
Available Options
86+
-----------------
87+
88+
.. include:: /reference/constraints/_groups-option.rst.inc
89+
90+
message
91+
~~~~~~~
92+
93+
**type**: ``string`` **default**: ``This value should be either negative or zero.``
94+
95+
The default message supplied when the value is not less than or equal to zero.
96+
97+
You can use the following parameters in this message:
98+
99+
============================= ================================================
100+
Parameter Description
101+
============================= ================================================
102+
``{{ compared_value }}`` Always zero
103+
``{{ compared_value_type }}`` The expected value type
104+
``{{ value }}`` The current (invalid) value
105+
============================= ================================================
106+
107+
.. include:: /reference/constraints/_payload-option.rst.inc

reference/constraints/Positive.rst

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
Positive
2+
========
3+
4+
.. versionadded:: 4.3
5+
6+
The ``Positive`` constraint was introduced in Symfony 4.3.
7+
8+
Validates that a value is a positive number. To force that a value is positive
9+
number or equal to zero, see :doc:`/reference/constraints/PositiveOrZero`.
10+
To force a value is negative, see :doc:`/reference/constraints/Negative`.
11+
12+
========== ===================================================================
13+
Applies to :ref:`property or method <validation-property-target>`
14+
Options - `groups`_
15+
- `message`_
16+
- `payload`_
17+
Class :class:`Symfony\\Component\\Validator\\Constraints\\Positive`
18+
Validator :class:`Symfony\\Component\\Validator\\Constraints\\GreaterThanValidator`
19+
========== ===================================================================
20+
21+
Basic Usage
22+
-----------
23+
24+
The following constraint ensure that:
25+
26+
* the ``income`` of an ``Employee`` is a positive number (greater than zero)
27+
28+
.. configuration-block::
29+
30+
.. code-block:: php-annotations
31+
32+
// src/Entity/Employee.php
33+
namespace App\Entity;
34+
35+
use Symfony\Component\Validator\Constraints as Assert;
36+
37+
class Employee
38+
{
39+
/**
40+
* @Assert\Positive
41+
*/
42+
protected $income;
43+
}
44+
45+
.. code-block:: yaml
46+
47+
# config/validator/validation.yaml
48+
App\Entity\Employee:
49+
properties:
50+
income:
51+
- Positive
52+
53+
.. code-block:: xml
54+
55+
<!-- config/validator/validation.xml -->
56+
<?xml version="1.0" encoding="UTF-8" ?>
57+
<constraint-mapping xmlns="http://symfony.com/schema/dic/constraint-mapping"
58+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
59+
xsi:schemaLocation="http://symfony.com/schema/dic/constraint-mapping https://symfony.com/schema/dic/constraint-mapping/constraint-mapping-1.0.xsd">
60+
61+
<class name="App\Entity\Employee">
62+
<property name="income">
63+
<constraint name="Positive"></constraint>
64+
</property>
65+
</class>
66+
</constraint-mapping>
67+
68+
.. code-block:: php
69+
70+
// src/Entity/Employee.php
71+
namespace App\Entity;
72+
73+
use Symfony\Component\Validator\Mapping\ClassMetadata;
74+
use Symfony\Component\Validator\Constraints as Assert;
75+
76+
class Employee
77+
{
78+
public static function loadValidatorMetadata(ClassMetadata $metadata)
79+
{
80+
$metadata->addPropertyConstraint('income', new Assert\Positive());
81+
}
82+
}
83+
84+
Available Options
85+
-----------------
86+
87+
.. include:: /reference/constraints/_groups-option.rst.inc
88+
89+
message
90+
~~~~~~~
91+
92+
**type**: ``string`` **default**: ``This value should be positive.``
93+
94+
The default message supplied when the value is not greater than zero.
95+
96+
You can use the following parameters in this message:
97+
98+
============================= ================================================
99+
Parameter Description
100+
============================= ================================================
101+
``{{ compared_value }}`` Always zero
102+
``{{ compared_value_type }}`` The expected value type
103+
``{{ value }}`` The current (invalid) value
104+
============================= ================================================
105+
106+
.. include:: /reference/constraints/_payload-option.rst.inc

0 commit comments

Comments
 (0)
0