@@ -88,6 +88,7 @@ in your form type::
88
88
use App\Painting\Color;
89
89
use Symfony\Component\Form\AbstractType;
90
90
use Symfony\Component\Form\DataMapperInterface;
91
+ use Symfony\Component\Form\Exception\TransformationFailedException;
91
92
use Symfony\Component\Form\Exception\UnexpectedTypeException;
92
93
use Symfony\Component\Form\FormInterface;
93
94
@@ -124,6 +125,17 @@ in your form type::
124
125
/** @var FormInterface[] $forms */
125
126
$forms = iterator_to_array($forms);
126
127
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
+
127
139
// as data is passed by reference, overriding it will change it in
128
140
// the form object as well
129
141
// beware of type inconsistency, see caution below
@@ -135,6 +147,10 @@ in your form type::
135
147
}
136
148
}
137
149
150
+ .. versionadded :: 4.3
151
+
152
+ The ``setInvalidMessage() `` method was introduced in Symfony 4.3.
153
+
138
154
.. caution ::
139
155
140
156
The data passed to the mapper is *not yet validated *. This means that your
0 commit comments