Skip to content

[Reference][Constraints]Added missing xml configuration examples for validation constraints #2053

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

Merged
Merged
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
16 changes: 16 additions & 0 deletions reference/constraints/All.rst
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,22 @@ entry in that array:
protected $favoriteColors = array();
}

.. code-block:: xml

<!-- src/Acme/UserBundle/Resources/config/validation.xml -->
<class name="Acme\UserBundle\Entity\User">
<property name="favoriteColors">
<constraint name="All">
<option name="constraints">
<constraint name="NotBlank" />
<constraint name="Length">
<option name="min">5</option>
</constraint>
</option>
</constraint>
</property>
</class>

Now, each entry in the ``favoriteColors`` array will be validated to not
be blank and to be at least 5 characters long.

Expand Down
4 changes: 2 additions & 2 deletions reference/constraints/Choice.rst
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ If your valid choice list is simple, you can pass them in directly via the
.. code-block:: xml

<!-- src/Acme/BlogBundle/Resources/config/validation.xml -->
<class name="Acme\BlogBundle\EntityAuthor">
<class name="Acme\BlogBundle\Entity\Author">
<property name="gender">
<constraint name="Choice">
<option name="choices">
Expand Down Expand Up @@ -280,4 +280,4 @@ strict

If true, the validator will also check the type of the input value. Specifically,
this value is passed to as the third argument to the PHP :phpfunction:`in_array` method
when checking to see if a value is in the valid choices array.
when checking to see if a value is in the valid choices array.
21 changes: 18 additions & 3 deletions reference/constraints/Count.rst
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ you might add the following:
- Count:
min: 1
max: 5
minMessage: You must specify at least one email
maxMessage: You cannot specify more than 5 emails
minMessage: "You must specify at least one email"
maxMessage: "You cannot specify more than {{ limit }} emails"

.. code-block:: php-annotations

Expand All @@ -53,12 +53,27 @@ you might add the following:
* min = "1",
* max = "5",
* minMessage = "You must specify at least one email",
* maxMessage = "You cannot specify more than 5 emails"
* maxMessage = "You cannot specify more than {{ limit }} emails"
* )
*/
protected $emails = array();
}

.. code-block:: xml

<!-- src/Acme/EventBundle/Resources/config/validation.xml -->
<class name="Acme\EventBundle\Entity\Participant">
<property name="emails">
<constraint name="Count">
<option name="min">1</option>
<option name="max">5</option>
<option name="minMessage">You must specify at least one email</option>
<option name="maxMessage">You cannot specify more than {{ limit }} emails</option>
</constraint>
</property>
</class>


Options
-------

Expand Down
9 changes: 9 additions & 0 deletions reference/constraints/Country.rst
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,15 @@ Basic Usage
protected $country;
}

.. code-block:: xml

<!-- src/Acme/UserBundle/Resources/config/validation.xml -->
<class name="Acme\UserBundle\Entity\User">
<property name="country">
<constraint name="Country" />
</property>
</class>

Options
-------

Expand Down
9 changes: 9 additions & 0 deletions reference/constraints/Date.rst
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,15 @@ Basic Usage
protected $birthday;
}

.. code-block:: xml

<!-- src/Acme/BlogBundle/Resources/config/validation.xml -->
<class name="Acme\BlogBundle\Entity\Author">
<property name="birthday">
<constraint name="Date" />
</property>
</class>

Options
-------

Expand Down
9 changes: 9 additions & 0 deletions reference/constraints/DateTime.rst
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,15 @@ Basic Usage
protected $createdAt;
}

.. code-block:: xml

<!-- src/Acme/UserBundle/Resources/config/validation.xml -->
<class name="Acme\BlogBundle\Entity\Author">
<property name="createdAt">
<constraint name="DateTime" />
</property>
</class>

Options
-------

Expand Down
11 changes: 11 additions & 0 deletions reference/constraints/False.rst
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,17 @@ method returns **false**:
- "False":
message: You've entered an invalid state.

.. code-block:: xml

<!-- src/Acme/BlogBundle/Resources/config/validation.xml -->
<class name="Acme\BlogBundle\Entity\Author">
<getter property="stateInvalid">
<constraint name="False">
<option name="message">You've entered an invalid state.</option>
</constraint>
</getter>
</class>

.. code-block:: php-annotations

// src/Acme/BlogBundle/Entity/Author.php
Expand Down
9 changes: 9 additions & 0 deletions reference/constraints/Ip.rst
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,15 @@ Basic Usage
protected $ipAddress;
}

.. code-block:: xml

<!-- src/Acme/BlogBundle/Resources/config/validation.xml -->
<class name="Acme\BlogBundle\Entity\Author">
<property name="ipAddress">
<constraint name="Ip" />
</property>
</class>

Options
-------

Expand Down
9 changes: 9 additions & 0 deletions reference/constraints/Language.rst
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,15 @@ Basic Usage
protected $preferredLanguage;
}

.. code-block:: xml

<!-- src/Acme/UserBundle/Resources/config/validation.xml -->
<class name="Acme\UserBundle\Entity\User">
<property name="preferredLanguage">
<constraint name="Language" />
</property>
</class>

Options
-------

Expand Down
2 changes: 1 addition & 1 deletion reference/constraints/Length.rst
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ To verify that the ``firstName`` field length of a class is between "2" and

.. code-block:: xml

<!-- src/Acme/BlogBundle/Resources/config/validation.xml -->
<!-- src/Acme/EventBundle/Resources/config/validation.xml -->
<class name="Acme\EventBundle\Entity\Participant">
<property name="firstName">
<constraint name="Length">
Expand Down
9 changes: 9 additions & 0 deletions reference/constraints/Locale.rst
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,15 @@ Basic Usage
protected $locale;
}

.. code-block:: xml

<!-- src/Acme/UserBundle/Resources/config/validation.xml -->
<class name="Acme\UserBundle\Entity\User">
<property name="locale">
<constraint name="Locale" />
</property>
</class>

Options
-------

Expand Down
12 changes: 12 additions & 0 deletions reference/constraints/Max.rst
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,18 @@ add the following:
protected $age;
}

.. code-block:: xml

<!-- src/Acme/EventBundle/Resources/config/validation.yml -->
<class name="Acme\EventBundle\Entity\Participant">
<property name="age">
<constraint name="Max">
<option name="limit">50</option>
<option name="message">You must be 50 or under to enter.</option>
</constraint>
</property>
</class>

Options
-------

Expand Down
14 changes: 14 additions & 0 deletions reference/constraints/Range.rst
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,20 @@ the following:
protected $height;
}

.. code-block:: xml

<!-- src/Acme/EventBundle/Resources/config/validation.xml -->
<class name="Acme\EventBundle\Entity\Participant">
<property name="height">
<constraint name="Range">
<option name="min">120</option>
<option name="max">180</option>
<option name="minMessage">You must be at least 120cm tall to enter</option>
<option name="maxMessage">You cannot be taller than 180cm to enter</option>
</constraint>
</property>
</class>

Options
-------

Expand Down
26 changes: 25 additions & 1 deletion reference/constraints/Regex.rst
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,17 @@ characters at the beginning of your string:
protected $description;
}

.. code-block:: xml

<!-- src/Acme/BlogBundle/Resources/config/validation.xml -->
<class name="Acme\BlogBundle\Entity\Author">
<property name="description">
<constraint name="Regex">
<option name="pattern">/^\w+/</option>
</constraint>
</property>
</class>

Alternatively, you can set the `match`_ option to ``false`` in order to assert
that a given string does *not* match. In the following example, you'll assert
that the ``firstName`` field does not contain any numbers and give it a custom
Expand Down Expand Up @@ -85,6 +96,19 @@ message:
protected $firstName;
}

.. code-block:: xml

<!-- src/Acme/BlogBundle/Resources/config/validation.xml -->
<class name="Acme\BlogBundle\Entity\Author">
<property name="firstName">
<constraint name="Regex">
<option name="pattern">/\d/</option>
<option name="match">false</option>
<option name="message">Your name cannot contain a number</option>
</constraint>
</property>
</class>

Options
-------

Expand Down Expand Up @@ -114,4 +138,4 @@ message

**type**: ``string`` **default**: ``This value is not valid``

This is the message that will be shown if this validator fails.
This is the message that will be shown if this validator fails.
9 changes: 9 additions & 0 deletions reference/constraints/Time.rst
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,15 @@ of the day when the event starts:
protected $startsAt;
}

.. code-block:: xml

<!-- src/Acme/EventBundle/Resources/config/validation.xml -->
<class name="Acme\EventBundle\Entity\Event">
<property name="startsAt">
<constraint name="Time" />
</property>
</class>

Options
-------

Expand Down
14 changes: 13 additions & 1 deletion reference/constraints/Type.rst
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,18 @@ Basic Usage
protected $age;
}

.. code-block:: xml

<!-- src/Acme/BlogBundle/Resources/config/validation.xml -->
<class name="Acme\BlogBundle\Entity\Author">
<property name="age">
<constraint name="Type">
<option name="type">integer</option>
<option name="message">The value {{ value }} is not a valid {{ type }}.</option>
</constraint>
</property>
</class>

Options
-------

Expand Down Expand Up @@ -80,4 +92,4 @@ message

**type**: ``string`` **default**: ``This value should be of type {{ type }}``

The message if the underlying data is not of the given type.
The message if the underlying data is not of the given type.
9 changes: 9 additions & 0 deletions reference/constraints/Url.rst
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,15 @@ Basic Usage
protected $bioUrl;
}

.. code-block:: xml

<!-- src/Acme/BlogBundle/Resources/config/validation.xml -->
<class name="Acme\BlogBundle\Entity\Author">
<property name="bioUrl">
<constraint name="Url" />
</property>
</class>

Options
-------

Expand Down