Skip to content

Added constraints PHP format and some code tweaks #2083

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 3 commits into from
Jan 3, 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
55 changes: 38 additions & 17 deletions reference/constraints/All.rst
Original file line number Diff line number Diff line change
Expand Up @@ -34,21 +34,21 @@ entry in that array:

.. code-block:: php-annotations

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

use Symfony\Component\Validator\Constraints as Assert;

class User
{
/**
* @Assert\All({
* @Assert\NotBlank
* @Assert\MinLength(5),
* })
*/
protected $favoriteColors = array();
}
// src/Acme/UserBundle/Entity/User.php
namespace Acme\UserBundle\Entity;
use Symfony\Component\Validator\Constraints as Assert;
class User
{
/**
* @Assert\All({
* @Assert\NotBlank
* @Assert\MinLength(5),
* })
*/
protected $favoriteColors = array();
}

.. code-block:: xml

Expand All @@ -58,14 +58,35 @@ entry in that array:
<constraint name="All">
<option name="constraints">
<constraint name="NotBlank" />
<constraint name="Length">
<option name="min">5</option>
<constraint name="MinLength">
<option name="limit">5</option>
</constraint>
</option>
</constraint>
</property>
</class>

.. code-block:: php

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

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

class User
{
public static function loadValidatorMetadata(ClassMetadata $metadata)
{
$metadata->addPropertyConstraint('favoriteColors', new Assert\All(array(
'constraints' => array(
new Assert\NotBlank(),
new Assert\MinLength(array('limit' => 5)),
),
)));
}
}

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

Expand Down
26 changes: 23 additions & 3 deletions reference/constraints/Blank.rst
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,17 @@ of an ``Author`` class were blank, you could do the following:

.. code-block:: yaml

properties:
firstName:
- Blank: ~
# src/BlogBundle/Resources/config/validation.yml
Acme\BlogBundle\Entity\Author:
properties:
firstName:
- Blank: ~

.. 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 @@ -52,6 +56,22 @@ of an ``Author`` class were blank, you could do the following:
</property>
</class>

.. code-block:: php

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

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

class Author
{
public static function loadValidatorMetadata(ClassMetadata $metadata)
{
$metadata->addPropertyConstraint('firstName', new Assetc\Blank());
Copy link
Contributor

Choose a reason for hiding this comment

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

Should be new Assert\Blank() probably ? :)

}
}

Options
-------

Expand Down
20 changes: 20 additions & 0 deletions reference/constraints/Callback.rst
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ Setup
.. code-block:: php-annotations

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

use Symfony\Component\Validator\Constraints as Assert;

/**
Expand All @@ -63,6 +65,24 @@ Setup
</constraint>
</class>

.. code-block:: php

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

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

class Author
{
public static function loadValidatorMetadata(ClassMetadata $metadata)
{
$metadata->addConstraint(new Assert\Callback(array(
'methods' => array('isAuthorValid'),
)));
}
}

The Callback Method
-------------------

Expand Down
10 changes: 7 additions & 3 deletions reference/constraints/Choice.rst
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ If your valid choice list is simple, you can pass them in directly via the
.. 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 @@ -77,16 +79,18 @@ If your valid choice list is simple, you can pass them in directly via the
.. code-block:: php

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

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

class Author
{
protected $gender;

public static function loadValidatorMetadata(ClassMetadata $metadata)
{
$metadata->addPropertyConstraint('gender', new Choice(array(
$metadata->addPropertyConstraint('gender', new Assert\Choice(array(
'choices' => array('male', 'female'),
'message' => 'Choose a valid gender',
)));
Expand Down Expand Up @@ -280,4 +284,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.
38 changes: 21 additions & 17 deletions reference/constraints/Collection.rst
Original file line number Diff line number Diff line change
Expand Up @@ -53,21 +53,25 @@ blank but is no longer than 100 characters in length, you would do the following

.. code-block:: yaml

properties:
profileData:
- Collection:
fields:
personal_email: Email
short_bio:
- NotBlank
- MaxLength:
limit: 100
message: Your short bio is too long!
allowMissingFields: true
# src/BlogBundle/Resources/config/validation.yml
Acme\BlogBundle\Entity\Author:
properties:
profileData:
- Collection:
fields:
personal_email: Email
short_bio:
- NotBlank
- MaxLength:
limit: 100
message: Your short bio is too long!
allowMissingFields: true

.. code-block:: php-annotations

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

use Symfony\Component\Validator\Constraints as Assert;

class Author
Expand Down Expand Up @@ -119,21 +123,21 @@ blank but is no longer than 100 characters in length, you would do the following
.. code-block:: php

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

use Symfony\Component\Validator\Mapping\ClassMetadata;
use Symfony\Component\Validator\Constraints\Collection;
use Symfony\Component\Validator\Constraints\Email;
use Symfony\Component\Validator\Constraints\MaxLength;
use Symfony\Component\Validator\Constraints as Assert;

class Author
{
private $options = array();

public static function loadValidatorMetadata(ClassMetadata $metadata)
{
$metadata->addPropertyConstraint('profileData', new Collection(array(
$metadata->addPropertyConstraint('profileData', new Assert\Collection(array(
'fields' => array(
'personal_email' => new Email(),
'lastName' => array(new NotBlank(), new MaxLength(100)),
'personal_email' => new Assert\Email(),
'lastName' => array(new Assert\NotBlank(), new Assert\MaxLength(100)),
),
'allowMissingFields' => true,
)));
Expand Down
40 changes: 28 additions & 12 deletions reference/constraints/Country.rst
Original file line number Diff line number Diff line change
Expand Up @@ -28,18 +28,18 @@ Basic Usage

.. code-block:: php-annotations

// src/Acme/UserBundle/Entity/User.php
namespace Acme\UserBundle\Entity;
use Symfony\Component\Validator\Constraints as Assert;

class User
{
/**
* @Assert\Country
*/
protected $country;
}
// src/Acme/UserBundle/Entity/User.php
namespace Acme\UserBundle\Entity;

use Symfony\Component\Validator\Constraints as Assert;

class User
{
/**
* @Assert\Country
*/
protected $country;
}

.. code-block:: xml

Expand All @@ -50,6 +50,22 @@ Basic Usage
</property>
</class>

.. code-block:: php

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

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

class User
{
public static function loadValidationMetadata(ClassMetadata $metadata)
{
$metadata->addPropertyConstraint('country', new Assert\Country());
}
}

Options
-------

Expand Down
18 changes: 18 additions & 0 deletions reference/constraints/Date.rst
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ Basic Usage
.. 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 @@ -50,6 +52,22 @@ Basic Usage
</property>
</class>

.. code-block:: php

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

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

class Author
{
public static function loadValidatorMetadata(ClassMetadata $metadata)
{
$metadata->addPropertyConstraint('birthday', new Assert\Date());
}
}

Options
-------

Expand Down
16 changes: 16 additions & 0 deletions reference/constraints/DateTime.rst
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,22 @@ Basic Usage
</property>
</class>

.. code-block:: php

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

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

class Author
{
public static function loadValidatorMetadata(ClassMetadata $metadata)
{
$metadata->addPropertyConstraint('createdAt', new Assert\DataTime());
}
}

Options
-------

Expand Down
21 changes: 20 additions & 1 deletion reference/constraints/Email.rst
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,25 @@ Basic Usage
protected $email;
}

.. code-block:: php

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

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

class Author
{
public static function loadValidatorMetadata(ClassMetadata $metadata)
{
$metadata->addPropertyConstraint('email', new Assert\Email(array(
'message' => 'The email "{{ value }}" is not a valid email.',
'check' => true,
)));
}
}

Options
-------

Expand All @@ -83,4 +102,4 @@ checkMX
If true, then the `checkdnsrr`_ PHP function will be used to check the validity
of the MX record of the host of the given email.

.. _`checkdnsrr`: http://www.php.net/manual/en/function.checkdnsrr.php
.. _`checkdnsrr`: http://www.php.net/manual/en/function.checkdnsrr.php
Loading