Skip to content

Commit ee627ff

Browse files
committed
Merge pull request symfony#1468 from aerialls/patch-3
[Cookbook][Validation] Updated the cookbook to the latest Symfony2.1 cha...
2 parents 8a38300 + 4dd35de commit ee627ff

File tree

1 file changed

+16
-19
lines changed

1 file changed

+16
-19
lines changed

cookbook/validation/custom_constraint.rst

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,17 @@ How to create a Custom Validation Constraint
55
============================================
66

77
You can create a custom constraint by extending the base constraint class,
8-
:class:`Symfony\\Component\\Validator\\Constraint`.
9-
As an example we're going to create a simple validator that checks if a string
8+
:class:`Symfony\\Component\\Validator\\Constraint`.
9+
As an example we're going to create a simple validator that checks if a string
1010
contains only alphanumeric characters.
1111

1212
Creating Constraint class
1313
-------------------------
1414

15-
First you need to create a Constraint class and extend :class:`Symfony\\Component\\Validator\\Constraint`::
15+
First you need to create a Constraint class and extend :class:`Symfony\\Component\\Validator\\Constraint`::
1616

1717
namespace Acme\DemoBundle\Validator\Constraints;
18-
18+
1919
use Symfony\Component\Validator\Constraint;
2020

2121
/**
@@ -31,11 +31,11 @@ First you need to create a Constraint class and extend :class:`Symfony\\Componen
3131
The ``@Annotation`` annotation is necessary for this new constraint in
3232
order to make it available for use in classes via annotations.
3333
Options for your constraint are represented as public properties on the
34-
constraint class.
34+
constraint class.
3535

3636
Creating the Validator itself
3737
-----------------------------
38-
38+
3939
As you can see, a constraint class is fairly minimal. The actual validation is
4040
performed by a another "constraint validator" class. The constraint validator
4141
class is specified by the constraint's ``validatedBy()`` method, which
@@ -54,7 +54,7 @@ when actually performing the validation.
5454
The validator class is also simple, and only has one required method: ``validate``::
5555

5656
namespace Acme\DemoBundle\Validator\Constraints;
57-
57+
5858
use Symfony\Component\Validator\Constraint;
5959
use Symfony\Component\Validator\ConstraintValidator;
6060

@@ -82,7 +82,7 @@ The validator class is also simple, and only has one required method: ``validate
8282
The ``isValid`` method was renamed to ``validate`` in Symfony 2.1. The
8383
``setMessage`` method was also deprecated, in favor of calling ``addViolation``
8484
on the context.
85-
85+
8686
Using the new Validator
8787
-----------------------
8888

@@ -91,7 +91,7 @@ Using custom validators is very easy, just as the ones provided by Symfony2 itse
9191
.. configuration-block::
9292

9393
.. code-block:: yaml
94-
94+
9595
# src/Acme/BlogBundle/Resources/config/validation.yml
9696
Acme\DemoBundle\Entity\AcmeEntity:
9797
properties:
@@ -102,25 +102,25 @@ Using custom validators is very easy, just as the ones provided by Symfony2 itse
102102
.. code-block:: php-annotations
103103
104104
// src/Acme/DemoBundle/Entity/AcmeEntity.php
105-
105+
106106
use Symfony\Component\Validator\Constraints as Assert;
107107
use Acme\DemoBundle\Validator\Constraints as AcmeAssert;
108-
108+
109109
class AcmeEntity
110110
{
111111
// ...
112-
112+
113113
/**
114114
* @Assert\NotBlank
115115
* @AcmeAssert\ContainsAlphanumeric
116116
*/
117117
protected $name;
118-
118+
119119
// ...
120120
}
121121
122122
.. code-block:: xml
123-
123+
124124
<!-- src/Acme/DemoBundle/Resources/config/validation.xml -->
125125
<?xml version="1.0" encoding="UTF-8" ?>
126126
<constraint-mapping xmlns="http://symfony.com/schema/dic/constraint-mapping"
@@ -136,7 +136,7 @@ Using custom validators is very easy, just as the ones provided by Symfony2 itse
136136
</constraint-mapping>
137137
138138
.. code-block:: php
139-
139+
140140
// src/Acme/DemoBundle/Entity/AcmeEntity.php
141141
142142
use Symfony\Component\Validator\Mapping\ClassMetadata;
@@ -223,10 +223,7 @@ With this, the validator ``validate()`` method gets an object as its first argum
223223
public function validate($protocol, Constraint $constraint)
224224
{
225225
if ($protocol->getFoo() != $protocol->getBar()) {
226-
227-
$propertyPath = $this->context->getPropertyPath() . 'foo';
228-
$this->context->setPropertyPath($propertyPath);
229-
$this->context->addViolation($constraint->getMessage(), array(), null);
226+
$this->context->addViolationAtSubPath('foo', $constraint->message, array(), null);
230227

231228
return false;
232229
}

0 commit comments

Comments
 (0)