|
| 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 ``withdraw`` of a bankaccount ``TransferItem`` 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 TransferItem |
| 39 | + { |
| 40 | + /** |
| 41 | + * @Assert\NegativeOrZero |
| 42 | + */ |
| 43 | + protected $withdraw; |
| 44 | + } |
| 45 | +
|
| 46 | + .. code-block:: yaml |
| 47 | +
|
| 48 | + # config/validator/validation.yaml |
| 49 | + App\Entity\TransferItem: |
| 50 | + properties: |
| 51 | + withdraw: |
| 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\TransferItem"> |
| 63 | + <property name="withdraw"> |
| 64 | + <constraint name="NegativeOrZero"></constraint> |
| 65 | + </property> |
| 66 | + </class> |
| 67 | + </constraint-mapping> |
| 68 | +
|
| 69 | + .. code-block:: php |
| 70 | +
|
| 71 | + // src/Entity/TransferItem.php |
| 72 | + namespace App\Entity; |
| 73 | +
|
| 74 | + use Symfony\Component\Validator\Mapping\ClassMetadata; |
| 75 | + use Symfony\Component\Validator\Constraints as Assert; |
| 76 | +
|
| 77 | + class TransferItem |
| 78 | + { |
| 79 | + public static function loadValidatorMetadata(ClassMetadata $metadata) |
| 80 | + { |
| 81 | + $metadata->addPropertyConstraint('withdraw', new Assert\NegativeOrZero(); |
| 82 | + } |
| 83 | + } |
0 commit comments