15
15
use Symfony \Component \Form \Extension \Validator \Util \ServerParams ;
16
16
use Symfony \Component \Validator \Constraint ;
17
17
use Symfony \Component \Validator \ConstraintValidator ;
18
+ use Symfony \Component \Validator \Context \ExecutionContextInterface ;
18
19
use Symfony \Component \Validator \Exception \UnexpectedTypeException ;
19
20
20
21
/**
@@ -53,7 +54,11 @@ public function validate($form, Constraint $constraint)
53
54
54
55
/* @var FormInterface $form */
55
56
$ config = $ form ->getConfig ();
56
- $ validator = $ this ->context ->getValidator ()->inContext ($ this ->context );
57
+ $ validator = null ;
58
+
59
+ if ($ this ->context instanceof ExecutionContextInterface) {
60
+ $ validator = $ this ->context ->getValidator ()->inContext ($ this ->context );
61
+ }
57
62
58
63
if ($ form ->isSynchronized ()) {
59
64
// Validate the form data only if transformation succeeded
@@ -62,7 +67,12 @@ public function validate($form, Constraint $constraint)
62
67
// Validate the data against its own constraints
63
68
if (self ::allowDataWalking ($ form )) {
64
69
foreach ($ groups as $ group ) {
65
- $ validator ->atPath ('data ' )->validate ($ form ->getData (), null , $ group );
70
+ if ($ validator ) {
71
+ $ validator ->atPath ('data ' )->validate ($ form ->getData (), null , $ group );
72
+ } else {
73
+ // 2.4 API
74
+ $ this ->context ->validate ($ form ->getData (), 'data ' , $ group , true );
75
+ }
66
76
}
67
77
}
68
78
@@ -72,7 +82,12 @@ public function validate($form, Constraint $constraint)
72
82
foreach ($ constraints as $ constraint ) {
73
83
foreach ($ groups as $ group ) {
74
84
if (in_array ($ group , $ constraint ->groups )) {
75
- $ validator ->atPath ('data ' )->validate ($ form ->getData (), $ constraint , $ group );
85
+ if ($ validator ) {
86
+ $ validator ->atPath ('data ' )->validate ($ form ->getData (), $ constraint , $ group );
87
+ } else {
88
+ // 2.4 API
89
+ $ this ->context ->validateValue ($ form ->getData (), $ constraint , 'data ' , $ group );
90
+ }
76
91
77
92
// Prevent duplicate validation
78
93
continue 2 ;
@@ -101,20 +116,40 @@ public function validate($form, Constraint $constraint)
101
116
? (string ) $ form ->getViewData ()
102
117
: gettype ($ form ->getViewData ());
103
118
104
- $ this ->context ->buildViolation ($ config ->getOption ('invalid_message ' ))
105
- ->setParameters (array_replace (array ('{{ value }} ' => $ clientDataAsString ), $ config ->getOption ('invalid_message_parameters ' )))
106
- ->setInvalidValue ($ form ->getViewData ())
107
- ->setCode (Form::ERR_INVALID )
108
- ->addViolation ();
119
+ if ($ this ->context instanceof ExecutionContextInterface) {
120
+ $ this ->context ->buildViolation ($ config ->getOption ('invalid_message ' ))
121
+ ->setParameters (array_replace (array ('{{ value }} ' => $ clientDataAsString ), $ config ->getOption ('invalid_message_parameters ' )))
122
+ ->setInvalidValue ($ form ->getViewData ())
123
+ ->setCode (Form::ERR_INVALID )
124
+ ->addViolation ();
125
+ } else {
126
+ // 2.4 API
127
+ $ this ->context ->addViolation (
128
+ $ config ->getOption ('invalid_message ' ),
129
+ array_replace (array ('{{ value }} ' => $ clientDataAsString ), $ config ->getOption ('invalid_message_parameters ' )),
130
+ $ form ->getViewData (),
131
+ null ,
132
+ Form::ERR_INVALID
133
+ );
134
+ }
109
135
}
110
136
}
111
137
112
138
// Mark the form with an error if it contains extra fields
113
139
if (count ($ form ->getExtraData ()) > 0 ) {
114
- $ this ->context ->buildViolation ($ config ->getOption ('extra_fields_message ' ))
115
- ->setParameter ('{{ extra_fields }} ' , implode ('", " ' , array_keys ($ form ->getExtraData ())))
116
- ->setInvalidValue ($ form ->getExtraData ())
117
- ->addViolation ();
140
+ if ($ this ->context instanceof ExecutionContextInterface) {
141
+ $ this ->context ->buildViolation ($ config ->getOption ('extra_fields_message ' ))
142
+ ->setParameter ('{{ extra_fields }} ' , implode ('", " ' , array_keys ($ form ->getExtraData ())))
143
+ ->setInvalidValue ($ form ->getExtraData ())
144
+ ->addViolation ();
145
+ } else {
146
+ // 2.4 API
147
+ $ this ->context ->addViolation (
148
+ $ config ->getOption ('extra_fields_message ' ),
149
+ array ('{{ extra_fields }} ' => implode ('", " ' , array_keys ($ form ->getExtraData ()))),
150
+ $ form ->getExtraData ()
151
+ );
152
+ }
118
153
}
119
154
120
155
// Mark the form with an error if the uploaded size was too large
@@ -124,10 +159,19 @@ public function validate($form, Constraint $constraint)
124
159
$ max = $ this ->serverParams ->getPostMaxSize ();
125
160
126
161
if (!empty ($ max ) && $ length > $ max ) {
127
- $ this ->context ->buildViolation ($ config ->getOption ('post_max_size_message ' ))
128
- ->setParameter ('{{ max }} ' , $ this ->serverParams ->getNormalizedIniPostMaxSize ())
129
- ->setInvalidValue ($ length )
130
- ->addViolation ();
162
+ if ($ this ->context instanceof ExecutionContextInterface) {
163
+ $ this ->context ->buildViolation ($ config ->getOption ('post_max_size_message ' ))
164
+ ->setParameter ('{{ max }} ' , $ this ->serverParams ->getNormalizedIniPostMaxSize ())
165
+ ->setInvalidValue ($ length )
166
+ ->addViolation ();
167
+ } else {
168
+ // 2.4 API
169
+ $ this ->context ->addViolation (
170
+ $ config ->getOption ('post_max_size_message ' ),
171
+ array ('{{ max }} ' => $ this ->serverParams ->getNormalizedIniPostMaxSize ()),
172
+ $ length
173
+ );
174
+ }
131
175
}
132
176
}
133
177
}
0 commit comments