-
-
Notifications
You must be signed in to change notification settings - Fork 5.2k
[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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not sure if we should include There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
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 ``level`` of a ``UnderGroundGarage`` is a negative number or equal to zero | ||
OskarStark marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
.. configuration-block:: | ||
|
||
.. code-block:: php-annotations | ||
|
||
// src/Entity/TransferItem.php | ||
namespace App\Entity; | ||
|
||
use Symfony\Component\Validator\Constraints as Assert; | ||
|
||
class UnderGroundGarage | ||
{ | ||
/** | ||
* @Assert\NegativeOrZero | ||
*/ | ||
protected $level; | ||
} | ||
|
||
.. code-block:: yaml | ||
|
||
# config/validator/validation.yaml | ||
App\Entity\UnderGroundGarage: | ||
properties: | ||
level: | ||
- 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\UnderGroundGarage"> | ||
<property name="level"> | ||
<constraint name="NegativeOrZero"></constraint> | ||
</property> | ||
</class> | ||
</constraint-mapping> | ||
|
||
.. code-block:: php | ||
|
||
// src/Entity/UnderGroundGarage.php | ||
namespace App\Entity; | ||
|
||
use Symfony\Component\Validator\Mapping\ClassMetadata; | ||
use Symfony\Component\Validator\Constraints as Assert; | ||
|
||
class UnderGroundGarage | ||
{ | ||
public static function loadValidatorMetadata(ClassMetadata $metadata) | ||
{ | ||
$metadata->addPropertyConstraint('level', new Assert\NegativeOrZero()); | ||
} | ||
} | ||
OskarStark marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
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 |
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 |
Uh oh!
There was an error while loading. Please reload this page.