Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Can't transform array data in PreSubmit event or DataTransformer #47093

Copy link
Copy link
Closed
@ghost

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.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      Morty Proxy This is a proxified and sanitized view of the page, visit original site.