symfony: [Form] CollectionType & ModelTransformer conflict

Symfony version(s) affected

5.4.4

Description

It’s not possible to transform data of CollectionType because ResizeFormListener() is called before and throw an UnexpectedTypeException : Expected argument of type “array or (\Traversable and \ArrayAccess)”, “string” given.

How to reproduce

With these files:

CollectionjsonType.php

<?php

namespace App\Form\Type;

use App\Form\DataTransformer\CollectionjsonTransformer;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\CollectionType;
use Symfony\Component\Form\FormBuilderInterface;

class CollectionjsonType extends AbstractType
{
    private $transformer;

    public function __construct(CollectionjsonTransformer $transformer)
    {
        $this->transformer = $transformer;
    }

    public function buildForm(FormBuilderInterface $builder, array $options): void
    {
        $builder->addModelTransformer($this->transformer);
    }

    public function getParent()
    {
        return CollectionType::class;
    }
}

CollectionjsonTransformer.php

<?php

namespace App\Form\DataTransformer;

use Symfony\Component\Form\DataTransformerInterface;

class CollectionjsonTransformer implements DataTransformerInterface
{
    public function transform($input)
    {
        return json_decode($input, true);
    }

    public function reverseTransform($input)
    {
        return json_encode($input);
    }
}

QuotationType.php

<?php

namespace App\Form;

use App\Form\Type\CollectionjsonType;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\FormEvent;
use Symfony\Component\Form\FormEvents;

class QuotationType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder->addEventListener(FormEvents::PRE_SET_DATA, [$this, 'onPreSetData']);
    }

    public function onPreSetData(FormEvent $event)
    {
        $data = $event->getData();
        $form = $event->getForm();

        if ($data->getForm()) {
            $form->add('jobs', CollectionjsonType::class, [
                'entry_type' => '\App\Form\Type\JobType',
                'allow_add' => true,
                'allow_delete' => true,
                'data' => '[{"job":"My Job","domain":"My Domain"}]',
            ]);
        }
    }
}

JobType .php

<?php

namespace App\Form\Type;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;

class JobType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options): void
    {
        $builder
            ->add('job', null, [
                'required' => true,
            ])
            ->add('domain', null, [
                'required' => true,
            ])
        ;
    }
}

Possible Solution

No response

Additional Context

If we replace 'data' => '[{"job":"My Job","domain":"My Domain"}]' by 'data' => json_decode('[{"job":"My Job","domain":"My Domain"}]', true), the exception UnexpectedTypeException is not throw and transform is called.

About this issue

  • Original URL
  • State: closed
  • Created 5 years ago
  • Reactions: 4
  • Comments: 20 (6 by maintainers)

Most upvoted comments

Confirming, that this is still an issue.

The preSetData method of the ResizeFormListener is raising an UnexpectedTypeException based on the original data and doesn’t take model transformations into account.

This is pretty much due to the fact, that the PRE_SET_DATA form event is raised before the model transformers are applied (see Form::setData()).

A solution I could imagine would be a new post transform data event just in between data transformation and the actual data-to-form mapping. Another would be to move the model transformations into PRE_SET_DATA form event listeners with an adequately low priority (as to run them after legacy pre set data event listeners), so event listeners that require transformed data can use an even lower priority.

Then the resize form listener could use the transformed data to create the child elements.

Until then, we are overriding the data model abstraction layer of our framework to provide custom data getters and setters just for the CollectionType. 😐

@carsonbot I hope you accidentally discover the exit door of your home as a mop robot, then fail to clean the dirt under the sofa.