Skip to content

complete constraint XML examples #2842

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 1 commit into from
Jul 28, 2013
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
30 changes: 18 additions & 12 deletions reference/constraints/All.rst
Original file line number Diff line number Diff line change
Expand Up @@ -54,18 +54,24 @@ entry in that 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>
<?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 http://symfony.com/schema/dic/constraint-mapping/constraint-mapping-1.0.xsd">

<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>
</constraint-mapping>

.. code-block:: php

Expand Down
16 changes: 11 additions & 5 deletions reference/constraints/Blank.rst
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,17 @@ of an ``Author`` class were blank, you could do the following:
.. code-block:: xml

<!-- src/Acme/BlogBundle/Resources/config/validation.xml -->
<class name="Acme\BlogBundle\Entity\Author">
<property name="firstName">
<constraint name="Blank" />
</property>
</class>
<?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 http://symfony.com/schema/dic/constraint-mapping/constraint-mapping-1.0.xsd">

<class name="Acme\BlogBundle\Entity\Author">
<property name="firstName">
<constraint name="Blank" />
</property>
</class>
</constraint-mapping>

.. code-block:: php

Expand Down
46 changes: 29 additions & 17 deletions reference/constraints/Callback.rst
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,19 @@ Setup
.. code-block:: xml

<!-- src/Acme/BlogBundle/Resources/config/validation.xml -->
<class name="Acme\BlogBundle\Entity\Author">
<constraint name="Callback">
<option name="methods">
<value>isAuthorValid</value>
</option>
</constraint>
</class>
<?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 http://symfony.com/schema/dic/constraint-mapping/constraint-mapping-1.0.xsd">

<class name="Acme\BlogBundle\Entity\Author">
<constraint name="Callback">
<option name="methods">
<value>isAuthorValid</value>
</option>
</constraint>
</class>
</constraint-mapping>

.. code-block:: php

Expand Down Expand Up @@ -159,16 +165,22 @@ process. Each method can be one of the following formats:
.. code-block:: xml

<!-- src/Acme/BlogBundle/Resources/config/validation.xml -->
<class name="Acme\BlogBundle\Entity\Author">
<constraint name="Callback">
<option name="methods">
<value>
<value>Acme\BlogBundle\MyStaticValidatorClass</value>
<value>isAuthorValid</value>
</value>
</option>
</constraint>
</class>
<?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 http://symfony.com/schema/dic/constraint-mapping/constraint-mapping-1.0.xsd">

<class name="Acme\BlogBundle\Entity\Author">
<constraint name="Callback">
<option name="methods">
<value>
<value>Acme\BlogBundle\MyStaticValidatorClass</value>
<value>isAuthorValid</value>
</value>
</option>
</constraint>
</class>
</constraint-mapping>

.. code-block:: php

Expand Down
26 changes: 16 additions & 10 deletions reference/constraints/CardScheme.rst
Original file line number Diff line number Diff line change
Expand Up @@ -40,16 +40,22 @@ on an object that will contain a credit card number.
.. code-block:: xml

<!-- src/Acme/SubscriptionBundle/Resources/config/validation.xml -->
<class name="Acme\SubscriptionBundle\Entity\Transaction">
<property name="cardNumber">
<constraint name="CardScheme">
<option name="schemes">
<value>VISA</value>
</option>
<option name="message">Your credit card number is invalid.</option>
</constraint>
</property>
</class>
<?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 http://symfony.com/schema/dic/constraint-mapping/constraint-mapping-1.0.xsd">

<class name="Acme\SubscriptionBundle\Entity\Transaction">
<property name="cardNumber">
<constraint name="CardScheme">
<option name="schemes">
<value>VISA</value>
</option>
<option name="message">Your credit card number is invalid.</option>
</constraint>
</property>
</class>
</constraint-mapping>

.. code-block:: php-annotations

Expand Down
74 changes: 46 additions & 28 deletions reference/constraints/Choice.rst
Original file line number Diff line number Diff line change
Expand Up @@ -64,17 +64,23 @@ 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\Entity\Author">
<property name="gender">
<constraint name="Choice">
<option name="choices">
<value>male</value>
<value>female</value>
</option>
<option name="message">Choose a valid gender.</option>
</constraint>
</property>
</class>
<?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 http://symfony.com/schema/dic/constraint-mapping/constraint-mapping-1.0.xsd">

<class name="Acme\BlogBundle\Entity\Author">
<property name="gender">
<constraint name="Choice">
<option name="choices">
<value>male</value>
<value>female</value>
</option>
<option name="message">Choose a valid gender.</option>
</constraint>
</property>
</class>
</constraint-mapping>

.. code-block:: php

Expand Down Expand Up @@ -149,13 +155,19 @@ constraint.
.. code-block:: xml

<!-- src/Acme/BlogBundle/Resources/config/validation.xml -->
<class name="Acme\BlogBundle\Entity\Author">
<property name="gender">
<constraint name="Choice">
<option name="callback">getGenders</option>
</constraint>
</property>
</class>
<?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 http://symfony.com/schema/dic/constraint-mapping/constraint-mapping-1.0.xsd">

<class name="Acme\BlogBundle\Entity\Author">
<property name="gender">
<constraint name="Choice">
<option name="callback">getGenders</option>
</constraint>
</property>
</class>
</constraint-mapping>

.. code-block:: php

Expand Down Expand Up @@ -208,16 +220,22 @@ you can pass the class name and the method as an array.
.. code-block:: xml

<!-- src/Acme/BlogBundle/Resources/config/validation.xml -->
<class name="Acme\BlogBundle\Entity\Author">
<property name="gender">
<constraint name="Choice">
<option name="callback">
<value>Util</value>
<value>getGenders</value>
</option>
</constraint>
</property>
</class>
<?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 http://symfony.com/schema/dic/constraint-mapping/constraint-mapping-1.0.xsd">

<class name="Acme\BlogBundle\Entity\Author">
<property name="gender">
<constraint name="Choice">
<option name="callback">
<value>Util</value>
<value>getGenders</value>
</option>
</constraint>
</property>
</class>
</constraint-mapping>

.. code-block:: php

Expand Down
44 changes: 25 additions & 19 deletions reference/constraints/Collection.rst
Original file line number Diff line number Diff line change
Expand Up @@ -101,25 +101,31 @@ blank but is no longer than 100 characters in length, you would do the following
.. code-block:: xml

<!-- src/Acme/BlogBundle/Resources/config/validation.xml -->
<class name="Acme\BlogBundle\Entity\Author">
<property name="profileData">
<constraint name="Collection">
<option name="fields">
<value key="personal_email">
<constraint name="Email" />
</value>
<value key="short_bio">
<constraint name="NotBlank" />
<constraint name="Length">
<option name="max">100</option>
<option name="maxMessage">Your bio is too long!</option>
</constraint>
</value>
</option>
<option name="allowMissingFields">true</option>
</constraint>
</property>
</class>
<?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 http://symfony.com/schema/dic/constraint-mapping/constraint-mapping-1.0.xsd">

<class name="Acme\BlogBundle\Entity\Author">
<property name="profileData">
<constraint name="Collection">
<option name="fields">
<value key="personal_email">
<constraint name="Email" />
</value>
<value key="short_bio">
<constraint name="NotBlank" />
<constraint name="Length">
<option name="max">100</option>
<option name="maxMessage">Your bio is too long!</option>
</constraint>
</value>
</option>
<option name="allowMissingFields">true</option>
</constraint>
</property>
</class>
</constraint-mapping>

.. code-block:: php

Expand Down
26 changes: 16 additions & 10 deletions reference/constraints/Count.rst
Original file line number Diff line number Diff line change
Expand Up @@ -64,16 +64,22 @@ you might add the following:
.. 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>
<?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 http://symfony.com/schema/dic/constraint-mapping/constraint-mapping-1.0.xsd">

<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>
</constraint-mapping>

.. code-block:: php

Expand Down
16 changes: 11 additions & 5 deletions reference/constraints/Country.rst
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,17 @@ Basic Usage
.. code-block:: xml

<!-- src/Acme/UserBundle/Resources/config/validation.xml -->
<class name="Acme\UserBundle\Entity\User">
<property name="country">
<constraint name="Country" />
</property>
</class>
<?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 http://symfony.com/schema/dic/constraint-mapping/constraint-mapping-1.0.xsd">

<class name="Acme\UserBundle\Entity\User">
<property name="country">
<constraint name="Country" />
</property>
</class>
</constraint-mapping>

.. code-block:: php

Expand Down
16 changes: 11 additions & 5 deletions reference/constraints/Date.rst
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,17 @@ Basic Usage
.. code-block:: xml

<!-- src/Acme/BlogBundle/Resources/config/validation.xml -->
<class name="Acme\BlogBundle\Entity\Author">
<property name="birthday">
<constraint name="Date" />
</property>
</class>
<?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 http://symfony.com/schema/dic/constraint-mapping/constraint-mapping-1.0.xsd">

<class name="Acme\BlogBundle\Entity\Author">
<property name="birthday">
<constraint name="Date" />
</property>
</class>
</constraint-mapping>

.. code-block:: php

Expand Down
Loading