Skip to content

Commit 1f2a9ba

Browse files
committed
Merge branch '4.4'
* 4.4: Add weeks in the options 'labels' [#11756] Added a bit more info on transformation errors [Form] Documented the setInvalidMessage() method for data transformers
2 parents 17170ed + 2dc55a9 commit 1f2a9ba

File tree

2 files changed

+40
-0
lines changed

2 files changed

+40
-0
lines changed

form/data_transformers.rst

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

294+
Whenever the transformer throws an exception, the ``invalid_message`` is shown
295+
to the user. Instead of showing the same message every time, you can set the
296+
end-user error message in the data transformer using the
297+
``setInvalidMessage()`` method. It also allows you to include user values::
298+
299+
// src/Form/DataTransformer/IssueToNumberTransformer.php
300+
namespace App\Form\DataTransformer;
301+
302+
use Symfony\Component\Form\DataTransformerInterface;
303+
use Symfony\Component\Form\Exception\TransformationFailedException;
304+
305+
class IssueToNumberTransformer implements DataTransformerInterface
306+
{
307+
// ...
308+
309+
public function reverseTransform($issueNumber)
310+
{
311+
// ...
312+
313+
if (null === $issue) {
314+
$privateErrorMessage = sprintf('An issue with number "%s" does not exist!', $issueNumber);
315+
$publicErrorMessage = 'The given "{{ value }}" value is not a valid issue number.';
316+
317+
$failure = new TransformationFailedException($privateErrorMessage);
318+
$failure->setInvalidMessage($publicErrorMessage, [
319+
'{{ value }}' => $issueNumber,
320+
]);
321+
322+
throw $failure;
323+
}
324+
325+
return $issue;
326+
}
327+
}
328+
329+
.. versionadded:: 4.3
330+
331+
The ``setInvalidMessage()`` method was introduced in Symfony 4.3.
332+
294333
That's it! As long as you're using :ref:`autowire <services-autowire>` and
295334
:ref:`autoconfigure <services-autoconfigure>`, Symfony will automatically
296335
know to pass your ``TaskType`` an instance of the ``IssueToNumberTransformer``.

reference/forms/types/dateinterval.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,7 @@ are ``null``, so they display the "humanized version" of the child names (``Inve
143143
'invert' => null,
144144
'years' => null,
145145
'months' => null,
146+
'weeks' => null,
146147
'days' => null,
147148
'hours' => null,
148149
'minutes' => null,

0 commit comments

Comments
 (0)