1515use Symfony \Component \Form \Extension \Validator \Util \ServerParams ;
1616use Symfony \Component \Validator \Constraint ;
1717use Symfony \Component \Validator \ConstraintValidator ;
18+ use Symfony \Component \Validator \Context \ExecutionContextInterface ;
1819use Symfony \Component \Validator \Exception \UnexpectedTypeException ;
1920
2021/**
@@ -53,7 +54,11 @@ public function validate($form, Constraint $constraint)
5354
5455/* @var FormInterface $form */
5556$ 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+ }
5762
5863if ($ form ->isSynchronized ()) {
5964// Validate the form data only if transformation succeeded
@@ -62,7 +67,12 @@ public function validate($form, Constraint $constraint)
6267// Validate the data against its own constraints
6368if (self ::allowDataWalking ($ form )) {
6469foreach ($ groupsas $ 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+ }
6676 }
6777 }
6878
@@ -72,7 +82,12 @@ public function validate($form, Constraint $constraint)
7282foreach ($ constraintsas $ constraint ) {
7383foreach ($ groupsas $ group ) {
7484if (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+ }
7691
7792// Prevent duplicate validation
7893continue 2 ;
@@ -101,20 +116,40 @@ public function validate($form, Constraint $constraint)
101116 ? (string )$ form ->getViewData ()
102117 :gettype ($ form ->getViewData ());
103118
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+ }
109135 }
110136 }
111137
112138// Mark the form with an error if it contains extra fields
113139if (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+ }
118153 }
119154
120155// Mark the form with an error if the uploaded size was too large
@@ -124,10 +159,19 @@ public function validate($form, Constraint $constraint)
124159$ max =$ this ->serverParams ->getPostMaxSize ();
125160
126161if (!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+ }
131175 }
132176 }
133177 }