Skip to content

[Validator] Add docs for number constraints #11254

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions reference/constraints.rst
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@ Validation Constraints Reference
constraints/Range
constraints/DivisibleBy

constraints/Positive
constraints/PositiveOrZero
constraints/Negative
constraints/NegativeOrZero

constraints/Date
constraints/DateTime
constraints/Time
Expand Down
106 changes: 106 additions & 0 deletions reference/constraints/Negative.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
Negative
========

.. versionadded:: 4.3

The ``Negative`` constraint was introduced in Symfony 4.3.

Validates that a value is a negative number. To force that a value is a negative
number or equal to zero, see :doc:`/reference/constraints/NegativeOrZero`.
To force a value is positive, see :doc:`/reference/constraints/Positive`.

========== ===================================================================
Applies to :ref:`property or method <validation-property-target>`
Options - `groups`_
- `message`_
- `payload`_
Class :class:`Symfony\\Component\\Validator\\Constraints\\Negative`
Validator :class:`Symfony\\Component\\Validator\\Constraints\\LesserThanValidator`
========== ===================================================================

Basic Usage
-----------

The following constraint ensure that:

* the ``withdraw`` of a bankaccount ``TransferItem`` is a negative number (lesser than zero)

.. configuration-block::

.. code-block:: php-annotations

// src/Entity/TransferItem.php
namespace App\Entity;

use Symfony\Component\Validator\Constraints as Assert;

class TransferItem
{
/**
* @Assert\Negative
*/
protected $withdraw;
}

.. code-block:: yaml

# config/validator/validation.yaml
App\Entity\TransferItem:
properties:
withdraw:
- Negative

.. code-block:: xml

<!-- config/validator/validation.xml -->
<?xml version="1.0" encoding="UTF-8" ?>
<constraint-mapping xmlns="http://symfony.com/schema/dic/constraint-mapping"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://symfony.com/schema/dic/constraint-mapping https://symfony.com/schema/dic/constraint-mapping/constraint-mapping-1.0.xsd">

<class name="App\Entity\TransferItem">
<property name="withdraw">
<constraint name="Negative"></constraint>
</property>
</class>
</constraint-mapping>

.. code-block:: php

// src/Entity/TransferItem.php
namespace App\Entity;

use Symfony\Component\Validator\Mapping\ClassMetadata;
use Symfony\Component\Validator\Constraints as Assert;

class TransferItem
{
public static function loadValidatorMetadata(ClassMetadata $metadata)
{
$metadata->addPropertyConstraint('withdraw', new Assert\Negative());
}
}

Available Options
-----------------

.. include:: /reference/constraints/_groups-option.rst.inc

message
~~~~~~~

**type**: ``string`` **default**: ``This value should be negative.``

The default message supplied when the value is not less than zero.

You can use the following parameters in this message:

============================= ================================================
Parameter Description
============================= ================================================
``{{ compared_value }}`` Always zero
``{{ compared_value_type }}`` The expected value type
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure if we should include compared_value and compared_value_type here. They are available, but it doesn't make so much sense to me to use them. WDYT?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not sure if we haven't added such (if available) parameters for other constraints.

Please help @xabbuh @javiereguiluz

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Even if they won't be used frequently, for consistency with the other constraints I'd prefer to keep them. We always try to document all parameters. Thanks.

``{{ value }}`` The current (invalid) value
============================= ================================================

.. include:: /reference/constraints/_payload-option.rst.inc
107 changes: 107 additions & 0 deletions reference/constraints/NegativeOrZero.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
NegativeOrZero
==============

.. versionadded:: 4.3

The ``NegativeOrZero`` constraint was introduced in Symfony 4.3.

Validates that a value is a negative number or equal to zero. To force that a value
is only a negative number, see :doc:`/reference/constraints/Negative`.
To force a value is positive or equal to zero,
see :doc:`/reference/constraints/PositiveOrZero`.

========== ===================================================================
Applies to :ref:`property or method <validation-property-target>`
Options - `groups`_
- `message`_
- `payload`_
Class :class:`Symfony\\Component\\Validator\\Constraints\\NegativeOrZero`
Validator :class:`Symfony\\Component\\Validator\\Constraints\\LesserThanOrEqualValidator`
========== ===================================================================

Basic Usage
-----------

The following constraint ensure that:

* the ``withdraw`` of a bankaccount ``TransferItem`` is a negative number or equal to zero

.. configuration-block::

.. code-block:: php-annotations

// src/Entity/TransferItem.php
namespace App\Entity;

use Symfony\Component\Validator\Constraints as Assert;

class TransferItem
{
/**
* @Assert\NegativeOrZero
*/
protected $withdraw;
}

.. code-block:: yaml

# config/validator/validation.yaml
App\Entity\TransferItem:
properties:
withdraw:
- NegativeOrZero

.. code-block:: xml

<!-- config/validator/validation.xml -->
<?xml version="1.0" encoding="UTF-8" ?>
<constraint-mapping xmlns="http://symfony.com/schema/dic/constraint-mapping"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://symfony.com/schema/dic/constraint-mapping https://symfony.com/schema/dic/constraint-mapping/constraint-mapping-1.0.xsd">

<class name="App\Entity\TransferItem">
<property name="withdraw">
<constraint name="NegativeOrZero"></constraint>
</property>
</class>
</constraint-mapping>

.. code-block:: php

// src/Entity/TransferItem.php
namespace App\Entity;

use Symfony\Component\Validator\Mapping\ClassMetadata;
use Symfony\Component\Validator\Constraints as Assert;

class TransferItem
{
public static function loadValidatorMetadata(ClassMetadata $metadata)
{
$metadata->addPropertyConstraint('withdraw', new Assert\NegativeOrZero());
}
}

Available Options
-----------------

.. include:: /reference/constraints/_groups-option.rst.inc

message
~~~~~~~

**type**: ``string`` **default**: ``This value should be either negative or zero.``

The default message supplied when the value is not less than or equal to zero.

You can use the following parameters in this message:

============================= ================================================
Parameter Description
============================= ================================================
``{{ compared_value }}`` Always zero
``{{ compared_value_type }}`` The expected value type
``{{ value }}`` The current (invalid) value
============================= ================================================

.. include:: /reference/constraints/_payload-option.rst.inc
106 changes: 106 additions & 0 deletions reference/constraints/Positive.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
Positive
========

.. versionadded:: 4.3

The ``Positive`` constraint was introduced in Symfony 4.3.

Validates that a value is a positive number. To force that a value is positive
number or equal to zero, see :doc:`/reference/constraints/PositiveOrZero`.
To force a value is negative, see :doc:`/reference/constraints/Negative`.

========== ===================================================================
Applies to :ref:`property or method <validation-property-target>`
Options - `groups`_
- `message`_
- `payload`_
Class :class:`Symfony\\Component\\Validator\\Constraints\\Positive`
Validator :class:`Symfony\\Component\\Validator\\Constraints\\GreaterThanValidator`
========== ===================================================================

Basic Usage
-----------

The following constraint ensure that:

* the ``income`` of an ``Employee`` is a positive number (greater than zero)

.. configuration-block::

.. code-block:: php-annotations

// src/Entity/Employee.php
namespace App\Entity;

use Symfony\Component\Validator\Constraints as Assert;

class Employee
{
/**
* @Assert\Positive
*/
protected $income;
}

.. code-block:: yaml

# config/validator/validation.yaml
App\Entity\Employee:
properties:
income:
- Positive

.. code-block:: xml

<!-- config/validator/validation.xml -->
<?xml version="1.0" encoding="UTF-8" ?>
<constraint-mapping xmlns="http://symfony.com/schema/dic/constraint-mapping"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://symfony.com/schema/dic/constraint-mapping https://symfony.com/schema/dic/constraint-mapping/constraint-mapping-1.0.xsd">

<class name="App\Entity\Employee">
<property name="income">
<constraint name="Positive"></constraint>
</property>
</class>
</constraint-mapping>

.. code-block:: php

// src/Entity/Employee.php
namespace App\Entity;

use Symfony\Component\Validator\Mapping\ClassMetadata;
use Symfony\Component\Validator\Constraints as Assert;

class Employee
{
public static function loadValidatorMetadata(ClassMetadata $metadata)
{
$metadata->addPropertyConstraint('income', new Assert\Positive());
}
}

Available Options
-----------------

.. include:: /reference/constraints/_groups-option.rst.inc

message
~~~~~~~

**type**: ``string`` **default**: ``This value should be positive.``

The default message supplied when the value is not greater than zero.

You can use the following parameters in this message:

============================= ================================================
Parameter Description
============================= ================================================
``{{ compared_value }}`` Always zero
``{{ compared_value_type }}`` The expected value type
``{{ value }}`` The current (invalid) value
============================= ================================================

.. include:: /reference/constraints/_payload-option.rst.inc
Loading