Skip to content
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

[Validator] Add shorter examples using the default option #6597

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all 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
18 changes: 18 additions & 0 deletions reference/constraints/Choice.rst
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@ If your valid choice list is simple, you can pass them in directly via the

class Author
{
/**
* @Assert\Choice({"New York", "Berlin", "Tokyo"})
*/
protected $city;

/**
* @Assert\Choice(choices = {"male", "female"}, message = "Choose a valid gender.")
*/
Expand All @@ -56,6 +61,7 @@ If your valid choice list is simple, you can pass them in directly via the
# src/AppBundle/Resources/config/validation.yml
AppBundle\Entity\Author:
properties:
city: [New York, Berlin, Tokyo]
gender:
- Choice:
choices: [male, female]
Expand All @@ -70,6 +76,13 @@ If your valid choice list is simple, you can pass them in directly via the
xsi:schemaLocation="http://symfony.com/schema/dic/constraint-mapping http://symfony.com/schema/dic/constraint-mapping/constraint-mapping-1.0.xsd">

<class name="AppBundle\Entity\Author">
<property name="city">
<constraint name="Choice">
<value>New York</value>
<value>Berlin</value>
<value>Tokyo</value>
</constraint>
</property>
<property name="gender">
<constraint name="Choice">
<option name="choices">
Expand All @@ -96,6 +109,11 @@ If your valid choice list is simple, you can pass them in directly via the

public static function loadValidatorMetadata(ClassMetadata $metadata)
{
$metadata->addPropertyConstraint(
'gender',
new Assert\Choice(array('New York', 'Berlin', 'Tokyo'))
);

$metadata->addPropertyConstraint('gender', new Assert\Choice(array(
'choices' => array('male', 'female'),
'message' => 'Choose a valid gender.',
Expand Down
18 changes: 16 additions & 2 deletions reference/constraints/EqualTo.rst
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ To force that a value is *not* equal, see :doc:`/reference/constraints/NotEqualT
Basic Usage
-----------

If you want to ensure that the ``age`` of a ``Person`` class is equal to
``20``, you could do the following:
If you want to ensure that the ``firstName`` of a ``Person`` class is equal to ``Mary``
and that the ``age`` is ``20``, you could do the following:

.. configuration-block::

Expand All @@ -41,6 +41,11 @@ If you want to ensure that the ``age`` of a ``Person`` class is equal to

class Person
{
/**
* @Assert\EqualTo("Mary")
*/
protected $firstName;

/**
* @Assert\EqualTo(
* value = 20
Expand All @@ -54,6 +59,8 @@ If you want to ensure that the ``age`` of a ``Person`` class is equal to
# src/AppBundle/Resources/config/validation.yml
AppBundle\Entity\Person:
properties:
firstName:
- EqualTo: Mary
age:
- EqualTo:
value: 20
Expand All @@ -67,6 +74,11 @@ If you want to ensure that the ``age`` of a ``Person`` class is equal to
xsi:schemaLocation="http://symfony.com/schema/dic/constraint-mapping http://symfony.com/schema/dic/constraint-mapping/constraint-mapping-1.0.xsd">

<class name="AppBundle\Entity\Person">
<property name="firstName">
<constraint name="EqualTo">
<value>Mary</value>
</constraint>
</property>
<property name="age">
<constraint name="EqualTo">
<option name="value">20</option>
Expand All @@ -87,6 +99,8 @@ If you want to ensure that the ``age`` of a ``Person`` class is equal to
{
public static function loadValidatorMetadata(ClassMetadata $metadata)
{
$metadata->addPropertyConstraint('firstName', new Assert\EqualTo('Mary'));

$metadata->addPropertyConstraint('age', new Assert\EqualTo(array(
'value' => 20,
)));
Expand Down
21 changes: 19 additions & 2 deletions reference/constraints/GreaterThan.rst
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,10 @@ than another value, see :doc:`/reference/constraints/LessThan`.
Basic Usage
-----------

If you want to ensure that the ``age`` of a ``Person`` class is greater
than ``18``, you could do the following:
The following constraints ensure that:
- the number of ``siblings`` of a ``Person`` is greater than ``5``
Copy link
Member

Choose a reason for hiding this comment

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

Please leave a blank line before the list items. Otherwise, it will be displayed like this:

rst_syntax_error

- the ``age`` of a ``Person`` class is greater than ``18``
Copy link
Member

Choose a reason for hiding this comment

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

we commonly use * as list indicator (can be fixed during the merge though)



.. configuration-block::

Expand All @@ -37,6 +39,12 @@ than ``18``, you could do the following:

class Person
{

/**
* @Assert\GreaterThan(5)
*/
protected $siblings;

/**
* @Assert\GreaterThan(
* value = 18
Expand All @@ -50,6 +58,8 @@ than ``18``, you could do the following:
# src/AppBundle/Resources/config/validation.yml
AppBundle\Entity\Person:
properties:
siblings:
- GreaterThan: 5
age:
- GreaterThan:
value: 18
Expand All @@ -63,6 +73,11 @@ than ``18``, you could do the following:
xsi:schemaLocation="http://symfony.com/schema/dic/constraint-mapping http://symfony.com/schema/dic/constraint-mapping/constraint-mapping-1.0.xsd">

<class name="AppBundle\Entity\Person">
<property name="siblings">
<constraint name="GreaterThan">
<value>5</value>
</constraint>
</property>
<property name="age">
<constraint name="GreaterThan">
<option name="value">18</option>
Expand All @@ -83,6 +98,8 @@ than ``18``, you could do the following:
{
public static function loadValidatorMetadata(ClassMetadata $metadata)
{
$metadata->addPropertyConstraint('siblings', new Assert\GreaterThan(5));

$metadata->addPropertyConstraint('age', new Assert\GreaterThan(array(
'value' => 18,
)));
Expand Down
19 changes: 17 additions & 2 deletions reference/constraints/GreaterThanOrEqual.rst
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,9 @@ in the options. To force that a value is greater than another value, see
Basic Usage
-----------

If you want to ensure that the ``age`` of a ``Person`` class is greater
than or equal to ``18``, you could do the following:
The following constraints ensure that:
- the number of ``siblings`` of a ``Person`` is greater than or equal to ``5``
Copy link
Member

Choose a reason for hiding this comment

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

And here a blank line too before the list items.

- the ``age`` of a ``Person`` class is greater than or equal to ``18``

.. configuration-block::

Expand All @@ -36,6 +37,11 @@ than or equal to ``18``, you could do the following:

class Person
{
/**
* @Assert\GreaterThanOrEqual(5)
*/
protected $siblings;

/**
* @Assert\GreaterThanOrEqual(
* value = 18
Expand All @@ -49,6 +55,8 @@ than or equal to ``18``, you could do the following:
# src/AppBundle/Resources/config/validation.yml
AppBundle\Entity\Person:
properties:
siblings:
- GreaterThanOrEqual: 5
age:
- GreaterThanOrEqual:
value: 18
Expand All @@ -62,6 +70,11 @@ than or equal to ``18``, you could do the following:
xsi:schemaLocation="http://symfony.com/schema/dic/constraint-mapping http://symfony.com/schema/dic/constraint-mapping/constraint-mapping-1.0.xsd">

<class name="AppBundle\Entity\Person">
<property name="siblings">
<constraint name="GreaterThanOrEqual">
<value>5</value>
</constraint>
</property>
<property name="age">
<constraint name="GreaterThanOrEqual">
<option name="value">18</option>
Expand All @@ -82,6 +95,8 @@ than or equal to ``18``, you could do the following:
{
public static function loadValidatorMetadata(ClassMetadata $metadata)
{
$metadata->addPropertyConstraint('siblings', new Assert\GreaterThanOrEqual(5));

$metadata->addPropertyConstraint('age', new Assert\GreaterThanOrEqual(array(
'value' => 18,
)));
Expand Down
10 changes: 8 additions & 2 deletions reference/constraints/IdenticalTo.rst
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,9 @@ To force that a value is *not* identical, see
Basic Usage
-----------

If you want to ensure that the ``age`` of a ``Person`` class is equal to
``20`` and an integer, you could do the following:
The following constraints ensure that:
- ``firstName`` of ``Person` class is equal to ``Mary`` *and* is a string
- ``age`` is equal to``20`` *and* is of type integer

.. configuration-block::

Expand All @@ -42,6 +43,11 @@ If you want to ensure that the ``age`` of a ``Person`` class is equal to

class Person
{
/**
* @Assert\IdenticalTo("Mary")
*/
protected $firstName;

/**
* @Assert\IdenticalTo(
* value = 20
Expand Down
20 changes: 18 additions & 2 deletions reference/constraints/LessThan.rst
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,9 @@ than another value, see :doc:`/reference/constraints/GreaterThan`.
Basic Usage
-----------

If you want to ensure that the ``age`` of a ``Person`` class is less than
``80``, you could do the following:
The following constraints ensure that:
- the number of ``siblings`` of a ``Person`` is less than or equal to ``5``
- ``age`` is less than``80``

.. configuration-block::

Expand All @@ -37,6 +38,12 @@ If you want to ensure that the ``age`` of a ``Person`` class is less than

class Person
{

/**
* @Assert\LessThan(5)
*/
protected $siblings;

/**
* @Assert\LessThan(
* value = 80
Expand All @@ -50,6 +57,8 @@ If you want to ensure that the ``age`` of a ``Person`` class is less than
# src/AppBundle/Resources/config/validation.yml
AppBundle\Entity\Person:
properties:
siblings:
- LessThan: 5
age:
- LessThan:
value: 80
Expand All @@ -63,6 +72,11 @@ If you want to ensure that the ``age`` of a ``Person`` class is less than
xsi:schemaLocation="http://symfony.com/schema/dic/constraint-mapping http://symfony.com/schema/dic/constraint-mapping/constraint-mapping-1.0.xsd">

<class name="AppBundle\Entity\Person">
<property name="siblings">
<constraint name="LessThan">
<value>5</value>
</constraint>
</property>
<property name="age">
<constraint name="LessThan">
<option name="value">80</option>
Expand All @@ -83,6 +97,8 @@ If you want to ensure that the ``age`` of a ``Person`` class is less than
{
public static function loadValidatorMetadata(ClassMetadata $metadata)
{
$metadata->addPropertyConstraint('siblings', new Assert\LessThan(5));

$metadata->addPropertyConstraint('age', new Assert\LessThan(array(
'value' => 80,
)));
Expand Down
19 changes: 17 additions & 2 deletions reference/constraints/LessThanOrEqual.rst
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,9 @@ the options. To force that a value is less than another value, see
Basic Usage
-----------

If you want to ensure that the ``age`` of a ``Person`` class is less than
or equal to ``80``, you could do the following:
The following constraints ensure that:
- the number of ``siblings`` of a ``Person`` is less than or equal to ``5``
- the ``age`` is less than or equal to ``80``

.. configuration-block::

Expand All @@ -36,6 +37,11 @@ or equal to ``80``, you could do the following:

class Person
{
/**
* @Assert\LessThanOrEqual(5)
*/
protected $siblings;

/**
* @Assert\LessThanOrEqual(
* value = 80
Expand All @@ -49,6 +55,8 @@ or equal to ``80``, you could do the following:
# src/AppBundle/Resources/config/validation.yml
AppBundle\Entity\Person:
properties:
siblings:
- LessThanOrEqual: 5
age:
- LessThanOrEqual:
value: 80
Expand All @@ -62,6 +70,11 @@ or equal to ``80``, you could do the following:
xsi:schemaLocation="http://symfony.com/schema/dic/constraint-mapping http://symfony.com/schema/dic/constraint-mapping/constraint-mapping-1.0.xsd">

<class name="AppBundle\Entity\Person">
<property name="siblings">
<constraint name="LessThanOrEqual">
<value>5</value>
</constraint>
</property>
<property name="age">
<constraint name="LessThanOrEqual">
<option name="value">80</option>
Expand All @@ -82,6 +95,8 @@ or equal to ``80``, you could do the following:
{
public static function loadValidatorMetadata(ClassMetadata $metadata)
{
$metadata->addPropertyConstraint('siblings', new Assert\LessThanOrEqual(5));

$metadata->addPropertyConstraint('age', new Assert\LessThanOrEqual(array(
'value' => 80,
)));
Expand Down
19 changes: 17 additions & 2 deletions reference/constraints/NotEqualTo.rst
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,10 @@ options. To force that a value is equal, see

Basic Usage
-----------
If you want to ensure that the ``firstName`` of a ``Person`` is not equal to
``Mary`` and that the ``age`` of a ``Person`` class is not ``15``, you could do
the following:

If you want to ensure that the ``age`` of a ``Person`` class is not equal
to ``15``, you could do the following:

.. configuration-block::

Expand All @@ -42,6 +43,11 @@ to ``15``, you could do the following:

class Person
{
/**
* @Assert\NotEqualTo("Mary")
*/
protected $firstName;

/**
* @Assert\NotEqualTo(
* value = 15
Expand All @@ -55,6 +61,8 @@ to ``15``, you could do the following:
# src/AppBundle/Resources/config/validation.yml
AppBundle\Entity\Person:
properties:
firstName:
- NotEqualTo: Mary
age:
- NotEqualTo:
value: 15
Expand All @@ -68,6 +76,11 @@ to ``15``, you could do the following:
xsi:schemaLocation="http://symfony.com/schema/dic/constraint-mapping http://symfony.com/schema/dic/constraint-mapping/constraint-mapping-1.0.xsd">

<class name="AppBundle\Entity\Person">
<property name="firstName">
<constraint name="NotEqualTo">
<value>Mary</value>
</constraint>
</property>
<property name="age">
<constraint name="NotEqualTo">
<option name="value">15</option>
Expand All @@ -88,6 +101,8 @@ to ``15``, you could do the following:
{
public static function loadValidatorMetadata(ClassMetadata $metadata)
{
$metadata->addPropertyConstraint('age', new Assert\NotEqualTo('Mary'));

$metadata->addPropertyConstraint('age', new Assert\NotEqualTo(array(
'value' => 15,
)));
Expand Down
Loading