12
12
namespace Symfony \Component \Form \Tests \Extension \Validator \Constraints ;
13
13
14
14
use Symfony \Component \Form \FormBuilder ;
15
+ use Symfony \Component \Validator \ConstraintViolation ;
15
16
use Symfony \Component \Form \Exception \TransformationFailedException ;
16
17
use Symfony \Component \Form \CallbackTransformer ;
17
18
use Symfony \Component \Form \FormInterface ;
@@ -182,7 +183,13 @@ public function testDontValidateIfNotSynchronized()
182
183
$ graphWalker = $ context ->getGraphWalker ();
183
184
$ object = $ this ->getMock ('\stdClass ' );
184
185
185
- $ form = $ this ->getBuilder ('name ' , '\stdClass ' , array ('invalid_message ' => 'Invalid! ' ))
186
+ $ form = $ this ->getBuilder ('name ' , '\stdClass ' , array (
187
+ 'invalid_message ' => 'invalid_message_key ' ,
188
+ // Invalid message parameters must be supported, because the
189
+ // invalid message can be a translation key
190
+ // see https://github.com/symfony/symfony/issues/5144
191
+ 'invalid_message_parameters ' => array ('{{ foo }} ' => 'bar ' ),
192
+ ))
186
193
->setData ($ object )
187
194
->addViewTransformer (new CallbackTransformer (
188
195
function ($ data ) { return $ data ; },
@@ -191,16 +198,26 @@ function () { throw new TransformationFailedException(); }
191
198
->getForm ();
192
199
193
200
// Launch transformer
194
- $ form ->bind (array () );
201
+ $ form ->bind (' foo ' );
195
202
196
203
$ graphWalker ->expects ($ this ->never ())
197
204
->method ('walkReference ' );
198
205
199
206
$ this ->validator ->initialize ($ context );
200
207
$ this ->validator ->validate ($ form , new Form ());
201
208
209
+ $ expectedViolation = new ConstraintViolation (
210
+ 'invalid_message_key ' ,
211
+ array ('{{ value }} ' => 'foo ' , '{{ foo }} ' => 'bar ' ),
212
+ 'Root ' ,
213
+ null ,
214
+ 'foo ' ,
215
+ null ,
216
+ Form::ERR_INVALID
217
+ );
218
+
202
219
$ this ->assertCount (1 , $ context ->getViolations ());
203
- $ this ->assertEquals (' Invalid! ' , $ context ->getViolations ()->get (0 )-> getMessage ( ));
220
+ $ this ->assertEquals ($ expectedViolation , $ context ->getViolations ()->get (0 ));
204
221
}
205
222
206
223
public function testDontValidateConstraintsIfNotSynchronized ()
@@ -517,6 +534,7 @@ private function getBuilder($name = 'name', $dataClass = null, array $options =
517
534
{
518
535
$ options = array_replace (array (
519
536
'constraints ' => array (),
537
+ 'invalid_message_parameters ' => array (),
520
538
), $ options );
521
539
522
540
return new FormBuilder ($ name , $ dataClass , $ this ->dispatcher , $ this ->factory , $ options );
0 commit comments