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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Constraints] Fixed incosistenties and reordered config blocks #2247

Merged
merged 1 commit into from Mar 8, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 12 additions & 0 deletions reference/constraints/Callback.rst
Expand Up @@ -158,6 +158,18 @@ 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>Acme\BlogBundle\MyStaticValidatorClass</value>
<value>isAuthorValid</value>
</option>
</constraint>
</class>

.. code-block:: php

// src/Acme/BlogBundle/Entity/Author.php
Expand Down
88 changes: 67 additions & 21 deletions reference/constraints/Choice.rst
Expand Up @@ -46,21 +46,6 @@ If your valid choice list is simple, you can pass them in directly via the
choices: [male, female]
message: Choose a valid gender.

.. 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>

.. code-block:: php-annotations

// src/Acme/BlogBundle/Entity/Author.php
Expand All @@ -76,6 +61,21 @@ If your valid choice list is simple, you can pass them in directly via the
protected $gender;
}

.. 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>

.. code-block:: php

// src/Acme/BlogBundle/EntityAuthor.php
Expand Down Expand Up @@ -108,6 +108,8 @@ form element.
.. code-block:: php

// src/Acme/BlogBundle/Entity/Author.php
namespace Acme\BlogBundle\Entity;

class Author
{
public static function getGenders()
Expand All @@ -132,6 +134,8 @@ constraint.
.. code-block:: php-annotations

// src/Acme/BlogBundle/Entity/Author.php
namespace Acme\BlogBundle\Entity;

use Symfony\Component\Validator\Constraints as Assert;

class Author
Expand All @@ -153,6 +157,26 @@ constraint.
</property>
</class>

.. code-block:: php

// src/Acme/BlogBundle/EntityAuthor.php
namespace Acme\BlogBundle\Entity;

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

class Author
{
protected $gender;

public static function loadValidatorMetadata(ClassMetadata $metadata)
{
$metadata->addPropertyConstraint('gender', new Assert\Choice(array(
'callback' => 'getGenders',
)));
}
}

If the static callback is stored in a different class, for example ``Util``,
you can pass the class name and the method as an array.

Expand All @@ -166,6 +190,21 @@ you can pass the class name and the method as an array.
gender:
- Choice: { callback: [Util, getGenders] }

.. code-block:: php-annotations

// src/Acme/BlogBundle/Entity/Author.php
namespace Acme\BlogBundle\Entity;

use Symfony\Component\Validator\Constraints as Assert;

class Author
{
/**
* @Assert\Choice(callback = {"Util", "getGenders"})
*/
protected $gender;
}

.. code-block:: xml

<!-- src/Acme/BlogBundle/Resources/config/validation.xml -->
Expand All @@ -180,17 +219,24 @@ you can pass the class name and the method as an array.
</property>
</class>

.. code-block:: php-annotations
.. code-block:: php

// src/Acme/BlogBundle/Entity/Author.php
use Symfony\Component\Validator\Constraints as Assert;
// src/Acme/BlogBundle/EntityAuthor.php
namespace Acme\BlogBundle\Entity;

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

class Author
{
/**
* @Assert\Choice(callback = {"Util", "getGenders"})
*/
protected $gender;

public static function loadValidatorMetadata(ClassMetadata $metadata)
{
$metadata->addPropertyConstraint('gender', new Assert\Choice(array(
'callback' => array('Util', 'getGenders'),
)));
}
}

Available Options
Expand Down
3 changes: 2 additions & 1 deletion reference/constraints/Collection.rst
Expand Up @@ -30,6 +30,7 @@ Basic Usage
The ``Collection`` constraint allows you to validate the different keys of
a collection individually. Take the following example::

// src/Acme/BlogBundle/Entity/Author.php
namespace Acme\BlogBundle\Entity;

class Author
Expand All @@ -53,7 +54,7 @@ blank but is no longer than 100 characters in length, you would do the following

.. code-block:: yaml

# src/BlogBundle/Resources/config/validation.yml
# src/Acme/BlogBundle/Resources/config/validation.yml
Acme\BlogBundle\Entity\Author:
properties:
profileData:
Expand Down
35 changes: 18 additions & 17 deletions reference/constraints/Email.rst
Expand Up @@ -29,23 +29,6 @@ Basic Usage
- Email:
message: The email "{{ value }}" is not a valid email.
checkMX: true
.. code-block:: xml

<!-- src/Acme/BlogBundle/Resources/config/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 http://symfony.com/schema/dic/constraint-mapping/constraint-mapping-1.0.xsd">

<class name="Acme\BlogBundle\Entity\Author">
<property name="email">
<constraint name="Email">
<option name="message">The email "{{ value }}" is not a valid email.</option>
<option name="checkMX">true</option>
</constraint>
</property>
</class>
</constraint-mapping>

.. code-block:: php-annotations

Expand All @@ -65,6 +48,24 @@ Basic Usage
protected $email;
}

.. code-block:: xml

<!-- src/Acme/BlogBundle/Resources/config/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 http://symfony.com/schema/dic/constraint-mapping/constraint-mapping-1.0.xsd">

<class name="Acme\BlogBundle\Entity\Author">
<property name="email">
<constraint name="Email">
<option name="message">The email "{{ value }}" is not a valid email.</option>
<option name="checkMX">true</option>
</constraint>
</property>
</class>
</constraint-mapping>

.. code-block:: php

// src/Acme/BlogBundle/Entity/Author.php
Expand Down
28 changes: 15 additions & 13 deletions reference/constraints/False.rst
Expand Up @@ -48,17 +48,6 @@ 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 All @@ -69,14 +58,27 @@ method returns **false**:
class Author
{
/**
* @Assert\False()
* @Assert\False(
* message = "You've entered an invalid state."
* )
*/
public function isStateInvalid($message = "You've entered an invalid state.")
public function isStateInvalid()
{
// ...
}
}

.. 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

// src/Acme/BlogBundle/Entity/Author.php
Expand Down
5 changes: 2 additions & 3 deletions reference/constraints/File.rst
Expand Up @@ -120,15 +120,14 @@ below a certain file size and a valid PDF, add the following:
// src/Acme/BlogBundle/Entity/Author.php
namespace Acme\BlogBundle\Entity;

// ...
use Symfony\Component\Validator\Mapping\ClassMetadata;
use Symfony\Component\Validator\Constraints\File;
use Symfony\Component\Validator\Constraints as Assert;

class Author
{
public static function loadValidatorMetadata(ClassMetadata $metadata)
{
$metadata->addPropertyConstraint('bioFile', new File(array(
$metadata->addPropertyConstraint('bioFile', new Assert\File(array(
'maxSize' => '1024k',
'mimeTypes' => array(
'application/pdf',
Expand Down
46 changes: 35 additions & 11 deletions reference/constraints/UniqueEntity.rst
Expand Up @@ -27,6 +27,16 @@ table:

.. configuration-block::

.. code-block:: yaml

# src/Acme/UserBundle/Resources/config/validation.yml
Acme\UserBundle\Entity\Author:
constraints:
- Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity: email
properties:
email:
- Email: ~

.. code-block:: php-annotations

// Acme/UserBundle/Entity/User.php
Expand Down Expand Up @@ -55,28 +65,42 @@ table:
// ...
}

.. code-block:: yaml

# src/Acme/UserBundle/Resources/config/validation.yml
Acme\UserBundle\Entity\Author:
constraints:
- Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity: email
properties:
email:
- Email: ~

.. code-block:: xml

<class name="Acme\UserBundle\Entity\Author">
<constraint name="Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity">
<option name="fields">email</option>
<option name="message">This email already exists.</option>
</constraint>
<property name="email">
<property name="email">
<constraint name="Email" />
</property>
</class>

.. code-block:: php


// Acme/UserBundle/Entity/User.php
namespace Acme\UserBundle\Entity;

use Symfony\Component\Validator\Constraints as Assert;

// DON'T forget this use statement!!!
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;

class Author
{
public static function loadValidatorMetadata(ClassMetadata $metadata)
{
$metadata->addConstraint(new UniqueEntity(array(
'fields' => 'email',
'message' => 'This email already exists.',
)));

$metadata->addPropertyConstraint(new Assert\Email());
}
}

Options
-------

Expand Down