@@ -5,17 +5,17 @@ How to create a Custom Validation Constraint
5
5
============================================
6
6
7
7
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
10
10
contains only alphanumeric characters.
11
11
12
12
Creating Constraint class
13
13
-------------------------
14
14
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 `::
16
16
17
17
namespace Acme\DemoBundle\Validator\Constraints;
18
-
18
+
19
19
use Symfony\Component\Validator\Constraint;
20
20
21
21
/**
@@ -31,11 +31,11 @@ First you need to create a Constraint class and extend :class:`Symfony\\Componen
31
31
The ``@Annotation `` annotation is necessary for this new constraint in
32
32
order to make it available for use in classes via annotations.
33
33
Options for your constraint are represented as public properties on the
34
- constraint class.
34
+ constraint class.
35
35
36
36
Creating the Validator itself
37
37
-----------------------------
38
-
38
+
39
39
As you can see, a constraint class is fairly minimal. The actual validation is
40
40
performed by a another "constraint validator" class. The constraint validator
41
41
class is specified by the constraint's ``validatedBy() `` method, which
@@ -54,7 +54,7 @@ when actually performing the validation.
54
54
The validator class is also simple, and only has one required method: ``validate ``::
55
55
56
56
namespace Acme\DemoBundle\Validator\Constraints;
57
-
57
+
58
58
use Symfony\Component\Validator\Constraint;
59
59
use Symfony\Component\Validator\ConstraintValidator;
60
60
@@ -82,7 +82,7 @@ The validator class is also simple, and only has one required method: ``validate
82
82
The ``isValid `` method was renamed to ``validate `` in Symfony 2.1. The
83
83
``setMessage `` method was also deprecated, in favor of calling ``addViolation ``
84
84
on the context.
85
-
85
+
86
86
Using the new Validator
87
87
-----------------------
88
88
@@ -91,7 +91,7 @@ Using custom validators is very easy, just as the ones provided by Symfony2 itse
91
91
.. configuration-block ::
92
92
93
93
.. code-block :: yaml
94
-
94
+
95
95
# src/Acme/BlogBundle/Resources/config/validation.yml
96
96
Acme\DemoBundle\Entity\AcmeEntity :
97
97
properties :
@@ -102,25 +102,25 @@ Using custom validators is very easy, just as the ones provided by Symfony2 itse
102
102
.. code-block :: php-annotations
103
103
104
104
// src/Acme/DemoBundle/Entity/AcmeEntity.php
105
-
105
+
106
106
use Symfony\Component\Validator\Constraints as Assert;
107
107
use Acme\DemoBundle\Validator\Constraints as AcmeAssert;
108
-
108
+
109
109
class AcmeEntity
110
110
{
111
111
// ...
112
-
112
+
113
113
/**
114
114
* @Assert\NotBlank
115
115
* @AcmeAssert\ContainsAlphanumeric
116
116
*/
117
117
protected $name;
118
-
118
+
119
119
// ...
120
120
}
121
121
122
122
.. code-block :: xml
123
-
123
+
124
124
<!-- src/Acme/DemoBundle/Resources/config/validation.xml -->
125
125
<?xml version =" 1.0" encoding =" UTF-8" ?>
126
126
<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
136
136
</constraint-mapping >
137
137
138
138
.. code-block :: php
139
-
139
+
140
140
// src/Acme/DemoBundle/Entity/AcmeEntity.php
141
141
142
142
use Symfony\Component\Validator\Mapping\ClassMetadata;
@@ -223,10 +223,7 @@ With this, the validator ``validate()`` method gets an object as its first argum
223
223
public function validate($protocol, Constraint $constraint)
224
224
{
225
225
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);
230
227
231
228
return false;
232
229
}
0 commit comments