diff --git a/form/data_mappers.rst b/form/data_mappers.rst index 15e66ce54b3..8bed0bd45c1 100644 --- a/form/data_mappers.rst +++ b/form/data_mappers.rst @@ -124,13 +124,27 @@ in your form type:: /** @var FormInterface[] $forms */ $forms = iterator_to_array($forms); + $colors = [ + 'red' => $forms['red']->getData(), + 'green' => $forms['green']->getData(), + 'blue' => $forms['blue']->getData(), + ]; + + // TransformationFailedException will be transformed into a FormError + foreach ($colors as $name => $color) { + if ($color > 256 || $color < 0) { + throw new TransformationFailedException(sprintf('Invalid color %s "%s" value', $name, $color)); + } + } + + // as data is passed by reference, overriding it will change it in // the form object as well // beware of type inconsistency, see caution below $viewData = new Color( - $forms['red']->getData(), - $forms['green']->getData(), - $forms['blue']->getData() + $colors['red'], + $colors['green'], + $colors['blue'] ); } }