File tree Expand file tree Collapse file tree 1 file changed +38
-0
lines changed Expand file tree Collapse file tree 1 file changed +38
-0
lines changed Original file line number Diff line number Diff line change @@ -291,6 +291,44 @@ and type-hint the new class::
291
291
// ...
292
292
}
293
293
294
+ Instead of defining the same ``invalid_message `` every time the form type is
295
+ used, you can set the end-user error message in the data transformer using the
296
+ ``setInvalidMessage() `` method::
297
+
298
+ // src/Form/DataTransformer/IssueToNumberTransformer.php
299
+ namespace App\Form\DataTransformer;
300
+
301
+ use Symfony\Component\Form\DataTransformerInterface;
302
+ use Symfony\Component\Form\Exception\TransformationFailedException;
303
+
304
+ class IssueToNumberTransformer implements DataTransformerInterface
305
+ {
306
+ // ...
307
+
308
+ public function reverseTransform($issueNumber)
309
+ {
310
+ // ...
311
+
312
+ if (null === $issue) {
313
+ $privateErrorMessage = sprintf('An issue with number "%s" does not exist!', $issueNumber);
314
+ $publicErrorMessage = 'The given "{{ value }}" value is not a valid issue number.';
315
+
316
+ $failure = new TransformationFailedException($privateErrorMessage);
317
+ $failure->setInvalidMessage($publicErrorMessage, [
318
+ '{{ value }}' => $issueNumber,
319
+ ]);
320
+
321
+ throw $failure;
322
+ }
323
+
324
+ return $issue;
325
+ }
326
+ }
327
+
328
+ .. versionadded :: 4.3
329
+
330
+ The ``setInvalidMessage() `` method was introduced in Symfony 4.3.
331
+
294
332
That's it! As long as you're using :ref: `autowire <services-autowire >` and
295
333
:ref: `autoconfigure <services-autoconfigure >`, Symfony will automatically
296
334
know to pass your ``TaskType `` an instance of the ``IssueToNumberTransformer ``.
You can’t perform that action at this time.
0 commit comments