|
| 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 | +=============== ============================================================== |
0 commit comments