Skip to content

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

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

Closed
wants to merge 2 commits into from
Closed
Changes from 1 commit
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
61 changes: 61 additions & 0 deletions reference/constraints/Type.rst
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,67 @@ option to validate this.
Basic Usage
-----------

.. configuration-block::

.. code-block:: php-annotations

// src/AppBundle/Entity/Author.php
namespace AppBundle\Entity;

use Symfony\Component\Validator\Constraints as Assert;

class Author
{
/**
* @Assert\Type("integer")
*/
protected $age;
}

.. code-block:: yaml

# src/AppBundle/Resources/config/validation.yml
AppBundle\Entity\Author:
properties:
age:
- Type: integer

.. code-block:: xml

<!-- src/AppBundle/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="AppBundle\Entity\Author">
<property name="age">
<constraint name="Type">
<type>integer</type>
</constraint>
</property>
</class>
</constraint-mapping>

.. code-block:: php

// src/AppBundle/Entity/Author.php
namespace AppBundle\Entity;

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

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

Configure more options
~~~~~~~~~~~~~~~~~~~~~~

.. configuration-block::

.. code-block:: php-annotations
Expand Down