Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Added documentation for comparison validators#2603

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Closed
danielholmes wants to merge1 commit intosymfony:masterfromdanielholmes:comparison_validators
Closed
Show file tree
Hide file tree
Changes fromall 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
108 changes: 108 additions & 0 deletionsreference/constraints/EqualTo.rst
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
EqualTo
=======

.. versionadded:: 2.3
The ``EqualTo`` validator was added in Symfony 2.3

Validates that a value is equal to some other defined value. It is equivalent
to a `` == `` comparison in PHP.

+----------------+-----------------------------------------------------------------------+
| Applies to | :ref:`property or method<validation-property-target>` |
+----------------+-----------------------------------------------------------------------+
| Options | - `value`_ |
| | - `message`_ |
+----------------+-----------------------------------------------------------------------+
| Class | :class:`Symfony\\Component\\Validator\\Constraints\\EqualTo` |
+----------------+-----------------------------------------------------------------------+
| Validator | :class:`Symfony\\Component\\Validator\\Constraints\\EqualToValidator` |
+----------------+-----------------------------------------------------------------------+

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

If you wanted to ensure that the ``age`` property of a ``Student`` class
is 18, you could do the following:

.. configuration-block::

.. code-block:: yaml

# src/Acme/AppBundle/Resources/config/validation.yml
Acme\AppBundle\Entity\Student:
properties:
age:
- EqualTo:
value: 18
message: Students for this course must be exactly 18 years old

.. code-block:: php-annotations

// src/Acme/AppBundle/Entity/Student.php
namespace Acme\AppBundle\Entity;

use Symfony\Component\Validator\Constraints as Assert;

class Student
{
/**
* @Assert\EqualTo(
* age = 18,
* message = "Students for this course must be exactly 18 years old"
* )
*/
protected $age;
}

.. code-block:: xml

<!-- src/Acme/AppBundle/Resources/config/validation.xml -->
<class name="Acme\AppBundle\Entity\Student">
<property name="age">
<constraint name="EqualTo">
<option name="value">18</option>
<option name="message">
Students for this course must be exactly 18 years old
</option>
</constraint>
</property>
</class>

.. code-block:: php

// src/Acme/AppBundle/Entity/Student.php
namespace Acme\AppBundle\Entity;

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

class Author
{
public static function loadValidatorMetadata(ClassMetadata $metadata)
{
$metadata->addPropertyConstraint('age', new Assert\EqualTo(array(
'value' => 18,
'message' => 'Students for this course must be exactly 18 years old'
)));
}
}

Options
-------

value
~~~~~

**type**: ``mixed`` [:ref:`default option<validation-default-option>`]

This required option is the comparison value. Validation will fail if the given
value doesn't equal this comparison value.
Copy link
Member

Choose a reason for hiding this comment

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

this option is always the same. Could you please use includes, like I did in my PR? That makes it easier to maintain


message
~~~~~~~

**type**: ``string`` **default**:
``This value should be equal to {{ compared_value }}.``
Copy link
Member

Choose a reason for hiding this comment

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

please put this right after thedefault:, even if it breakes our line length rul.


This is the message that will be shown if the value doesn't equal the `value`_
option.
108 changes: 108 additions & 0 deletionsreference/constraints/GreaterThan.rst
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
GreaterThan
===========

.. versionadded:: 2.3
The ``GreaterThan`` validator was added in Symfony 2.3

Validates that a value is greater than some other defined value. It is equivalent
to a `` > `` comparison in PHP.

+----------------+---------------------------------------------------------------------------+
| Applies to | :ref:`property or method<validation-property-target>` |
+----------------+---------------------------------------------------------------------------+
| Options | - `value`_ |
| | - `message`_ |
+----------------+---------------------------------------------------------------------------+
| Class | :class:`Symfony\\Component\\Validator\\Constraints\\GreaterThan` |
+----------------+---------------------------------------------------------------------------+
| Validator | :class:`Symfony\\Component\\Validator\\Constraints\\GreaterThanValidator` |
+----------------+---------------------------------------------------------------------------+

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

If you wanted to ensure that the ``age`` property of a ``Student`` class
is greater than 18, you could do the following:

.. configuration-block::

.. code-block:: yaml

# src/Acme/AppBundle/Resources/config/validation.yml
Acme\AppBundle\Entity\Student:
properties:
age:
- GreaterThan:
value: 18
message: Students for this course must be over 18 years old

.. code-block:: php-annotations

// src/Acme/AppBundle/Entity/Student.php
namespace Acme\AppBundle\Entity;

use Symfony\Component\Validator\Constraints as Assert;

class Student
{
/**
* @Assert\GreaterThan(
* age = 18,
* message = "Students for this course must be over 18 years old"
* )
*/
protected $age;
}

.. code-block:: xml

<!-- src/Acme/AppBundle/Resources/config/validation.xml -->
<class name="Acme\AppBundle\Entity\Student">
<property name="age">
<constraint name="GreaterThan">
<option name="value">18</option>
<option name="message">
Students for this course must be over 18 years old
</option>
</constraint>
</property>
</class>

.. code-block:: php

// src/Acme/AppBundle/Entity/Student.php
namespace Acme\AppBundle\Entity;

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

class Author
{
public static function loadValidatorMetadata(ClassMetadata $metadata)
{
$metadata->addPropertyConstraint('age', new Assert\GreaterThan(array(
'value' => 18,
'message' => 'Students for this course must be over 18 years old'
)));
}
}

Options
-------

value
~~~~~

**type**: ``mixed`` [:ref:`default option<validation-default-option>`]

This required option is the comparison value. Validation will fail if the
given value is less than or equal to this comparison value.

message
~~~~~~~

**type**: ``string`` **default**:
``This value should be greater than {{ compared_value }}.``

This is the message that will be shown if the value is less than or equal
to the `value`_ option.
108 changes: 108 additions & 0 deletionsreference/constraints/GreaterThanOrEqual.rst
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
GreaterThanOrEqual
==================

.. versionadded:: 2.3
The ``GreaterThanOrEqual`` validator was added in Symfony 2.3

Validates that a value is greater than or equal to some other defined value.
It is equivalent to a `` >= `` comparison in PHP.

+----------------+----------------------------------------------------------------------------------+
| Applies to | :ref:`property or method<validation-property-target>` |
+----------------+----------------------------------------------------------------------------------+
| Options | - `value`_ |
| | - `message`_ |
+----------------+----------------------------------------------------------------------------------+
| Class | :class:`Symfony\\Component\\Validator\\Constraints\\GreaterThanOrEqual` |
+----------------+----------------------------------------------------------------------------------+
| Validator | :class:`Symfony\\Component\\Validator\\Constraints\\GreaterThanOrEqualValidator` |
+----------------+----------------------------------------------------------------------------------+

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

If you wanted to ensure that the ``age`` property of a ``Student`` class
is greater than or equal to 18, you could do the following:

.. configuration-block::

.. code-block:: yaml

# src/Acme/AppBundle/Resources/config/validation.yml
Acme\AppBundle\Entity\Student:
properties:
age:
- GreaterThanOrEqual:
value: 18
message: Students for this course must be 18 years old or over

.. code-block:: php-annotations

// src/Acme/AppBundle/Entity/Student.php
namespace Acme\AppBundle\Entity;

use Symfony\Component\Validator\Constraints as Assert;

class Student
{
/**
* @Assert\GreaterThanOrEqual(
* age = 18,
* message = "Students for this course must be 18 years old or over"
* )
*/
protected $age;
}

.. code-block:: xml

<!-- src/Acme/AppBundle/Resources/config/validation.xml -->
<class name="Acme\AppBundle\Entity\Student">
<property name="age">
<constraint name="GreaterThanOrEqual">
<option name="value">18</option>
<option name="message">
Students for this course must be 18 years old or over
</option>
</constraint>
</property>
</class>

.. code-block:: php

// src/Acme/AppBundle/Entity/Student.php
namespace Acme\AppBundle\Entity;

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

class Author
{
public static function loadValidatorMetadata(ClassMetadata $metadata)
{
$metadata->addPropertyConstraint('age', new Assert\GreaterThanOrEqual(array(
'value' => 18,
'message' => 'Students for this course must be 18 years old or over'
)));
}
}

Options
-------

value
~~~~~

**type**: ``mixed`` [:ref:`default option<validation-default-option>`]

This required option is the comparison value. Validation will fail if the
given value is less than this comparison value.

message
~~~~~~~

**type**: ``string`` **default**:
``This value should be greater than or equal to {{ compared_value }}.``

This is the message that will be shown if the value is less than the `value`_
option.
Loading

[8]ページ先頭

©2009-2025 Movatter.jp