Skip to content

Commit 861ca12

Browse files
committed
Moved the setInvalidMessage() from data mappers to data transformers
1 parent 40bd267 commit 861ca12

File tree

2 files changed

+38
-16
lines changed

2 files changed

+38
-16
lines changed

form/data_mappers.rst

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,6 @@ in your form type::
8888
use App\Painting\Color;
8989
use Symfony\Component\Form\AbstractType;
9090
use Symfony\Component\Form\DataMapperInterface;
91-
use Symfony\Component\Form\Exception\TransformationFailedException;
9291
use Symfony\Component\Form\Exception\UnexpectedTypeException;
9392
use Symfony\Component\Form\FormInterface;
9493

@@ -125,17 +124,6 @@ in your form type::
125124
/** @var FormInterface[] $forms */
126125
$forms = iterator_to_array($forms);
127126

128-
// optionally you can validate the data to throw more precise exception errors
129-
$redComponent = $forms['red']->getData();
130-
if (!\is_numeric($redComponent) || $redComponent < 0 || $redComponent > 255) {
131-
$failure = new TransformationFailedException('Expected a RGB color component.');
132-
$failure->setInvalidMessage('Color components should be numeric values in the [0-255] range. {{ value }} is invalid.', [
133-
'{{ value }}' => $redComponent,
134-
]);
135-
136-
throw $failure;
137-
}
138-
139127
// as data is passed by reference, overriding it will change it in
140128
// the form object as well
141129
// beware of type inconsistency, see caution below
@@ -147,10 +135,6 @@ in your form type::
147135
}
148136
}
149137

150-
.. versionadded:: 4.3
151-
152-
The ``setInvalidMessage()`` method was introduced in Symfony 4.3.
153-
154138
.. caution::
155139

156140
The data passed to the mapper is *not yet validated*. This means that your

form/data_transformers.rst

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,44 @@ and type-hint the new class::
291291
// ...
292292
}
293293

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+
294332
That's it! As long as you're using :ref:`autowire <services-autowire>` and
295333
:ref:`autoconfigure <services-autoconfigure>`, Symfony will automatically
296334
know to pass your ``TaskType`` an instance of the ``IssueToNumberTransformer``.

0 commit comments

Comments
 (0)