File tree Expand file tree Collapse file tree 2 files changed +42
-0
lines changed Expand file tree Collapse file tree 2 files changed +42
-0
lines changed Original file line number Diff line number Diff line change @@ -8,6 +8,7 @@ Validation Constraints Reference
8
8
constraints/False
9
9
constraints/True
10
10
constraints/Type
11
+ constraints/Callback
11
12
constraints/Choice
12
13
constraints/Collection
13
14
constraints/Date
@@ -38,6 +39,7 @@ The following constraints are natively available in Symfony2:
38
39
* :doc: `False <constraints/False >`
39
40
* :doc: `True <constraints/True >`
40
41
* :doc: `Type <constraints/Type >`
42
+ * :doc: `Callback <constraints/Callback >`
41
43
* :doc: `Choice <constraints/Choice >`
42
44
* :doc: `Collection <constraints/Collection >`
43
45
* :doc: `Date <constraints/Date >`
Original file line number Diff line number Diff line change
1
+ Callback
2
+ ========
3
+
4
+ Calls methods during validation on the object. These methods can then perform
5
+ any type of validation and assign errors where needed:
6
+
7
+ .. code-block :: yaml
8
+
9
+ Acme\DemoBundle\Entity\Author :
10
+ constraints :
11
+ - Callback :
12
+ methods : [isAuthorValid]
13
+
14
+ Usage
15
+ -----
16
+
17
+ The callback method is passed a special ``ExecutionContext `` object::
18
+
19
+ use Symfony\Component\Validator\ExecutionContext;
20
+
21
+ private $firstName;
22
+
23
+ public function isAuthorValid(ExecutionContext $context)
24
+ {
25
+ // somehow you get an array of "fake names"
26
+ $fakeNames = array();
27
+
28
+ // check if the name is actually a fake name
29
+ if (in_array($this->getFirstName(), $fakeNames)) {
30
+ $property_path = $context->getPropertyPath() . '.firstName';
31
+ $context->setPropertyPath($property_path);
32
+ $context->addViolation('This name sounds totally fake', array(), null);
33
+ }
34
+ }
35
+
36
+ Options
37
+ -------
38
+
39
+ * ``methods ``: The method names that should be executed as callbacks.
40
+ * ``message ``: The error message if the validation fails
You can’t perform that action at this time.
0 commit comments