@@ -35,8 +35,8 @@ Configuration
35
35
36
36
.. code-block :: php-annotations
37
37
38
- // src/Acme/BlogBundle /Entity/Author.php
39
- namespace Acme\BlogBundle \Entity;
38
+ // src/AppBundle /Entity/Author.php
39
+ namespace AppBundle \Entity;
40
40
41
41
use Symfony\Component\Validator\Constraints as Assert;
42
42
use Symfony\Component\Validator\Context\ExecutionContextInterface;
@@ -56,28 +56,28 @@ Configuration
56
56
57
57
.. code-block :: yaml
58
58
59
- # src/Acme/BlogBundle /Resources/config/validation.yml
60
- Acme\BlogBundle \Entity\Author :
59
+ # src/AppBundle /Resources/config/validation.yml
60
+ AppBundle \Entity\Author :
61
61
constraints :
62
62
- Callback : [validate]
63
63
64
64
.. code-block :: xml
65
65
66
- <!-- src/Acme/BlogBundle /Resources/config/validation.xml -->
66
+ <!-- src/AppBundle /Resources/config/validation.xml -->
67
67
<?xml version =" 1.0" encoding =" UTF-8" ?>
68
68
<constraint-mapping xmlns =" http://symfony.com/schema/dic/constraint-mapping"
69
69
xmlns : xsi =" http://www.w3.org/2001/XMLSchema-instance"
70
70
xsi : schemaLocation =" http://symfony.com/schema/dic/constraint-mapping http://symfony.com/schema/dic/constraint-mapping/constraint-mapping-1.0.xsd" >
71
71
72
- <class name =" Acme\BlogBundle \Entity\Author" >
72
+ <class name =" AppBundle \Entity\Author" >
73
73
<constraint name =" Callback" >validate</constraint >
74
74
</class >
75
75
</constraint-mapping >
76
76
77
77
.. code-block :: php
78
78
79
- // src/Acme/BlogBundle /Entity/Author.php
80
- namespace Acme\BlogBundle \Entity;
79
+ // src/AppBundle /Entity/Author.php
80
+ namespace AppBundle \Entity;
81
81
82
82
use Symfony\Component\Validator\Mapping\ClassMetadata;
83
83
use Symfony\Component\Validator\Constraints as Assert;
@@ -185,57 +185,69 @@ You can then use the following configuration to invoke this validator:
185
185
186
186
.. code-block :: php-annotations
187
187
188
- // src/Acme/BlogBundle /Entity/Author.php
188
+ // src/AppBundle /Entity/Author.php
189
189
namespace Acme\BlogBundle\Entity;
190
190
191
191
use Symfony\Component\Validator\Constraints as Assert;
192
192
193
193
/**
194
- * @Assert\Callback({"Vendor\Package\Validator ", "validate "})
194
+ * @Assert\Callback({"AppBundle\MyStaticValidatorClass ", "isAuthorValid "})
195
195
*/
196
196
class Author
197
197
{
198
198
}
199
199
200
200
.. code-block :: yaml
201
201
202
- # src/Acme/BlogBundle/Resources/config/validation.yml
203
- Acme\BlogBundle\Entity\Author :
204
- constraints :
205
- - Callback : [Vendor\Package\Validator, validate]
202
+ # src/AppBundle/Resources/config/validation.yml
203
+ AppBundle\E ntity\A uthor:
204
+ constraints:
205
+ - Callback:
206
+ methods:
207
+ - [AppBundle\M yStaticValidatorClass, isAuthorValid]
206
208
207
209
.. code-block :: xml
208
210
209
- <!-- src/Acme/BlogBundle/Resources/config/validation.xml -->
210
- <?xml version =" 1.0" encoding =" UTF-8" ?>
211
- <constraint-mapping xmlns =" http://symfony.com/schema/dic/constraint-mapping"
212
- xmlns : xsi =" http://www.w3.org/2001/XMLSchema-instance"
213
- xsi : schemaLocation =" http://symfony.com/schema/dic/constraint-mapping http://symfony.com/schema/dic/constraint-mapping/constraint-mapping-1.0.xsd" >
214
-
215
- <class name =" Acme\BlogBundle\Entity\Author" >
216
- <constraint name =" Callback" >
217
- <value >Vendor\Package\Validator</value >
218
- <value >validate</value >
219
- </constraint >
220
- </class >
221
- </constraint-mapping >
211
+ <!-- src/AppBundle/Resources/config/validation.xml -->
212
+ <?xml version="1.0" encoding="UTF-8" ?>
213
+ <constraint-mapping xmlns="http://symfony.com/schema/dic/constraint-mapping"
214
+ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
215
+ xsi:schemaLocation="http://symfony.com/schema/dic/constraint-mapping http://symfony.com/schema/dic/constraint-mapping/constraint-mapping-1.0.xsd">
216
+
217
+ <class name="AppBundle\E ntity\A uthor">
218
+ <constraint name="Callback">
219
+ <option name="methods">
220
+ <value>
221
+ <value>AppBundle\M yStaticValidatorClass</value>
222
+ <value>isAuthorValid</value>
223
+ </value>
224
+ </option>
225
+ </constraint>
226
+ </class>
227
+ </constraint-mapping>
222
228
223
229
.. code-block :: php
224
230
225
- // src/Acme/BlogBundle /Entity/Author.php
226
- namespace Acme\BlogBundle \Entity;
231
+ // src/AppBundle /Entity/Author.php
232
+ namespace AppBundle \E ntity;
227
233
228
234
use Symfony\C omponent\V alidator\M apping\C lassMetadata;
229
235
use Symfony\C omponent\V alidator\C onstraints as Assert;
230
236
231
237
class Author
232
238
{
239
+ public $name;
240
+
233
241
public static function loadValidatorMetadata(ClassMetadata $metadata)
234
242
{
235
- $metadata->addConstraint(new Assert\Callback(array(
236
- 'Vendor\Package\Validator',
237
- 'validate',
238
- )));
243
+ $metadata->addConstraint(new Callback(array(
244
+ 'methods' => array(
245
+ array(
246
+ 'AppBundle\M yStaticValidatorClass',
247
+ 'isAuthorValid',
248
+ ),
249
+ ),
250
+ )));
239
251
}
240
252
}
241
253
@@ -250,8 +262,11 @@ You can then use the following configuration to invoke this validator:
250
262
When configuring the constraint via PHP, you can also pass a closure to the
251
263
constructor of the Callback constraint::
252
264
253
- // src/Acme/BlogBundle/Entity/Author.php
254
- namespace Acme\BlogBundle\Entity;
265
+ // src/AppBundle/Entity/Author.php
266
+ namespace AppBundle\Entity;
267
+
268
+ use Symfony\Component\Validator\ExecutionContextInterface;
269
+ use AppBundle\Entity\Author;
255
270
256
271
use Symfony\Component\Validator\Mapping\ClassMetadata;
257
272
use Symfony\Component\Validator\Constraints as Assert;
0 commit comments