8000 [Validator] Add Finite constraint · symfony/symfony-docs@74c74b7 · GitHub
[go: up one dir, main page]

Skip to content

Commit 74c74b7

Browse files
committed
[Validator] Add Finite constraint
1 parent b31d45e commit 74c74b7

File tree

3 files changed

+110
-0
lines changed

3 files changed

+110
-0
lines changed

reference/constraints.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ Validation Constraints Reference
1111
constraints/IsNull
1212
constraints/IsTrue
1313
constraints/IsFalse
14+
constraints/IsFinite
1415
constraints/Type
1516

1617
constraints/Email

reference/constraints/IsFinite.rst

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
IsFinite
2+
========
3+
4+
Validates that a value is a legal finite number.
5+
6+
Also see :doc:`Type <Type>`.
7+
8+
========== ===================================================================
9+
Applies to :ref:`property or method <validation-property-target>`
10+
Class :class:`Symfony\\Component\\Validator\\Constraints\\IsFinite`
11+
Validator :class:`Symfony\\Component\\Validator\\Constraints\\IsFiniteValidator`
12+
========== ===================================================================
13+
14+
Basic Usage
15+
-----------
16+
17+
Since ``is_float`` considers ``INF`` and ``NAN`` as ``true``, you can narrow validation with ``IsFinite``
18+
19+
This constraint can be applied to properties (e.g. a ``price`` property
20+
on a product model) and methods. For example, suppose
21+
you have the following method::
22+
23+
// src/Entity/Product.php
24+
namespace App\Entity;
25+
26+
class Product
27+
{
28+
protected float $price;
29+
}
30+
31+
If you try to validate as follow:
32+
33+
.. configuration-block::
34+
35+
.. code-block:: php-attributes
36+
37+
// src/Entity/Product.php
38+
namespace App\Entity;
39+
40+
class Product
41+
{
42+
#[Assert\Type(['float'])]
43+
#[Assert\Finite]
44+
public float $price;
45+
}
46+
47+
.. code-block:: yaml
48+
49+
# config/validator/validation.yaml
50+
App\Entity\Product:
51+
getters:
52+
price:
53+
- Type: float
54+
- Finite: ~
55+
56+
.. code-block:: xml
57+
58+
<!-- config/validator/validation.xml -->
59+
<?xml version="1.0" encoding="UTF-8" ?>
60+
<constraint-mapping xmlns="http://symfony.com/schema/dic/constraint-mapping"
61+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
62+
xsi:schemaLocation="http://symfony.com/schema/dic/constraint-mapping https://symfony.com/schema/dic/constraint-mapping/constraint-mapping-1.0.xsd">
63+
64+
<class name="App\Entity\Product">
65+
<getter property="price">
66+
<constraint name="Type">
67+
<option name="type">float</option>
68+
</constraint>
69+
<constraint name="Finite"></constraint>
70+
</getter>
71+
</class>
72+
</constraint-mapping>
73+
74+
.. code-block:: php
75+
76+
// src/Entity/Product.php
77+
namespace App\Entity;
78+
79+
class Product
80+
{
81+
public static function loadValidatorMetadata(ClassMetadata $metadata): void
82+
{
83+
$metadata->addGetterConstraint('price', new Type(['float']));
84+
$metadata->addGetterConstraint('price', new IsFinite());
85+
}
86+
87+
public float $price;
88+
}
89+
90+
.. include:: /reference/constraints/_null-values-are-valid.rst.inc
91+
92+
Options
93+
-------
94+
95+
``message``
96+
~~~~~~~~~~~
97+
98+
**type**: ``string`` **default**: ``This value should be finite.``
99+
100+
This message is shown if the underlying data is not finite.
101+
102+
You can use the following parameters in this message:
103+
104+
=============== ==============================================================
105+
Parameter Description
106+
=============== ==============================================================
107+
``{{ value }}`` The current (invalid) value
108+
=============== ==============================================================

reference/constraints/map.rst.inc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ the value of properties or the return value of methods on your object.
1010
* :doc:`IsNull </reference/constraints/IsNull>`
1111
* :doc:`IsTrue </reference/constraints/IsTrue>`
1212
* :doc:`IsFalse </reference/constraints/IsFalse>`
13+
* :doc:`IsFinite </reference/constraints/IsFinite>`
1314
* :doc:`Type </reference/constraints/Type>`
1415

1516
String Constraints

0 commit comments

Comments
 (0)
0