Closed

Description
Symfony version(s) affected
5.4.9 and above
Description
In Form.php there is a check whether the submitted data is an array and if that is the case it is mandated that the form is compound or has the multiple attribute set to true. This check is done before handling the PRE_SUBMIT
event, which prevents transforming the submitted array to a single value in an event handler (or other data transformers).
How to reproduce
<?php
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\FormEvent;
use Symfony\Component\Form\FormEvents;
class F extends AbstractType {
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('foo', TextType::class);
$builder->get('foo')->addEventListener(FormEvents::PRE_SUBMIT, function (FormEvent $e) {
$e->setData($e->getData()['bar']);
});
}
}
// …
$f = $this->createForm(F::class);
$f->submit(['foo' => ['bar' => 'buz']]);
Possible Solution
Move the check for compound or multiple, after handling PRE_SUBMIT
and other data transformation has been handled.
Additional Context
In my case data is submitted as json via XMLHttpRequest rather than Html Forms.
symfony/form < 5.4.9 would unintendedly allow arrays if the multiple attribute was present but false.