New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Form] Enhanced CollectionType with *dynamic* types #28692

Open
FabienPapet opened this Issue Oct 2, 2018 · 1 comment

Comments

Projects
None yet
2 participants
@FabienPapet

FabienPapet commented Oct 2, 2018

Description
Enhanced CollectionType with dynamic types

Example
While making web apps for customers, I usually have the same code that is shared along projects:
The use case is simple,

Assuming I have a Setting object with three properties:

<?php

namespace App\Model;

class Setting
{
    public $id;
    public $type; // can be 'text' 'text' or other
    public $value;
}

If I want to build a custom Form with NumberType for number TextType for text, I always use the following:

<?php

namespace App\Form;

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

class ExtendedFieldCollectionType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder->addEventListener(FormEvents::PRE_SET_DATA, function (FormEvent $event) {
            $form = $event->getForm();
            $data = $event->getData();

            foreach ($data as $name => $value) {
               $type = .....  // depending on type
                $form->add($value->id, $type, [
                    'property_path' => '[' . $name . '].value', // the value property is my setting value
                ]);
            }
        });
    }
}

This is not possible (I think) with CollectionType:

  • because of the ResizeFormListener which fixes the type to use.
  • because the form field name is form[0][value] and I want something like form[identifier] as field name

So I think a new Form type based on the CollectionType can be a solution
Something configurable like field_type as a callback and field_value for the value

WDYT ? I can propose a PR

Here's a related Stackoverflow question: https://stackoverflow.com/a/52390309/1617144

@FabienPapet FabienPapet changed the title from Enhanced CollectionType with *dynamic* types to [Form] Enhanced CollectionType with *dynamic* types Oct 2, 2018

@xabbuh

This comment has been minimized.

Show comment
Hide comment
@xabbuh

xabbuh Oct 3, 2018

Member

This sounds like #9835 which was requested in the past and which we considered as something that better fits a third-party bundle. That being said I am still not convinced this belongs into the core.

Member

xabbuh commented Oct 3, 2018

This sounds like #9835 which was requested in the past and which we considered as something that better fits a third-party bundle. That being said I am still not convinced this belongs into the core.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment