@@ -74,7 +74,7 @@ One way to accomplish this is with the Expression constraint:
7474
7575 // src/Acme/DemoBundle/Model/BlogPost.php
7676 namespace Acme\DemoBundle\Model\BlogPost;
77-
77+
7878 use Symfony\Component\Validator\Constraints as Assert;
7979
8080 /**
@@ -91,23 +91,27 @@ One way to accomplish this is with the Expression constraint:
9191 .. code-block :: xml
9292
9393 <!-- src/Acme/DemoBundle/Resources/config/validation.xml -->
94- <class name =" Acme\DemoBundle\Model\BlogPost" >
95- <constraint name =" Expression" >
96- <option name =" expression" >
97- this.getCategory() in ['php', 'symfony'] or !this.isTechnicalPost()
98- </option >
99- <option name =" message" >
100- If this is a tech post, the category should be either php or symfony!
101- </option >
102- </constraint >
103- </class >
104-
94+ <?xml version =" 1.0" encoding =" UTF-8" ?>
95+ <constraint-mapping xmlns =" http://symfony.com/schema/dic/constraint-mapping"
96+ xmlns : xsi =" http://www.w3.org/2001/XMLSchema-instance"
97+ xsi : schemaLocation =" http://symfony.com/schema/dic/constraint-mapping http://symfony.com/schema/dic/constraint-mapping/constraint-mapping-1.0.xsd" >
98+ <class name =" Acme\DemoBundle\Model\BlogPost" >
99+ <constraint name =" Expression" >
100+ <option name =" expression" >
101+ this.getCategory() in ['php', 'symfony'] or !this.isTechnicalPost()
102+ </option >
103+ <option name =" message" >
104+ If this is a tech post, the category should be either php or symfony!
105+ </option >
106+ </constraint >
107+ </class >
108+ </constraint-mapping >
105109
106110 .. code-block :: php
107111
108112 // src/Acme/DemoBundle/Model/BlogPost.php
109- namespace Acme\DemoBundle\Model\BlogPost ;
110-
113+ namespace Acme\DemoBundle\Model;
114+
111115 use Symfony\Component\Validator\Mapping\ClassMetadata;
112116 use Symfony\Component\Validator\Constraints as Assert;
113117
@@ -129,6 +133,90 @@ expression that must return true in order for validation to pass. To learn
129133more about the expression language syntax, see
130134:doc: `/components/expression_language/syntax `.
131135
136+ .. sidebar :: Mapping the Error to a Specific Field
137+
138+ You can also attach the constraint to a specific property and still validate
139+ based on the values of the entire entity. This is handy if you want to attach
140+ the error to a specific field. In this context, ``value `` represents the value
141+ of ``isTechnicalPost ``.
142+
143+ .. configuration-block ::
144+
145+ .. code-block :: yaml
146+
147+ # src/Acme/DemoBundle/Resources/config/validation.yml
148+ Acme\DemoBundle\Model\BlogPost :
149+ properties :
150+ isTechnicalPost :
151+ - Expression :
152+ expression : " this.getCategory() in ['php', 'symfony'] or value == false"
153+ message : " If this is a tech post, the category should be either php or symfony!"
154+
155+ .. code-block :: php-annotations
156+
157+ // src/Acme/DemoBundle/Model/BlogPost.php
158+ namespace Acme\DemoBundle\Model;
159+
160+ use Symfony\Component\Validator\Constraints as Assert;
161+
162+ class BlogPost
163+ {
164+ // ...
165+
166+ /**
167+ * @Assert\Expression(
168+ * "this.getCategory() in ['php', 'symfony'] or value == false",
169+ * message="If this is a tech post, the category should be either php or symfony!"
170+ * )
171+ */
172+ private $isTechnicalPost;
173+
174+ // ...
175+ }
176+
177+ .. code-block :: xml
178+
179+ <!-- src/Acme/DemoBundle/Resources/config/validation.xml -->
180+ <?xml version =" 1.0" encoding =" UTF-8" ?>
181+ <constraint-mapping xmlns =" http://symfony.com/schema/dic/constraint-mapping"
182+ xmlns : xsi =" http://www.w3.org/2001/XMLSchema-instance"
183+ xsi : schemaLocation =" http://symfony.com/schema/dic/constraint-mapping http://symfony.com/schema/dic/constraint-mapping/constraint-mapping-1.0.xsd" >
184+
185+ <class name =" Acme\DemoBundle\Model\BlogPost" >
186+ <property name =" isTechnicalPost" >
187+ <constraint name =" Expression" >
188+ <option name =" expression" >
189+ this.getCategory() in ['php', 'symfony'] or value == false
190+ </option >
191+ <option name =" message" >
192+ If this is a tech post, the category should be either php or symfony!
193+ </option >
194+ </constraint >
195+ </property >
196+ </class >
197+ </constraint-mapping >
198+
199+ .. code-block :: php
200+
201+ // src/Acme/DemoBundle/Model/BlogPost.php
202+ namespace Acme\DemoBundle\Model;
203+
204+ use Symfony\Component\Validator\Constraints as Assert;
205+ use Symfony\Component\Validator\Mapping\ClassMetadata;
206+
207+ class BlogPost
208+ {
209+ public static function loadValidatorMetadata(ClassMetadata $metadata)
210+ {
211+ $metadata->addPropertyConstraint('isTechnicalPost', new Assert\Expression(array(
212+ 'expression' => 'this.getCategory() in ["php", "symfony"] or value == false',
213+ 'message' => 'If this is a tech post, the category should be either php or symfony!',
214+ )));
215+ }
216+
217+ // ...
218+ }
219+
132220 For more information about the expression and what variables are available
133221to you, see the :ref: `expression <reference-constraint-expression-option >`
134222option details below.
0 commit comments