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

[Form] Collection prototype does not contain elements added by events. #4367

Copy link
Copy link
Closed
@HelloGrayson

Description

@HelloGrayson
Issue body actions

I'm trying to setup a dynamic form collection using events based on the documentation.

First I add the collection to the form:

<?php

$builder->add('properties', 'collection', array(
    'type' => new Property(),
    'allow_add' => true,
    'allow_delete' => true,
    'prototype' => true,
    'label' => 'Properties',
));

Then in the Property form, I add an event subscriber:

<?php

$subscriber = new AddDynamicFieldsSubscriber($builder->getFormFactory());
$builder->addEventSubscriber($subscriber);

Then my subscriber class:

<?php

class AddDynamicFieldsSubscriber implements EventSubscriberInterface
{
    private $factory;

    public function __construct(FormFactoryInterface $factory)
    {
        $this->factory = $factory;
    }
    public static function getSubscribedEvents()
    {
        return array(FormEvents::PRE_SET_DATA => 'preSetData');
    }
    public function preSetData(DataEvent $event)
    {
        $data = $event->getData();
        $form = $event->getForm();

        if (null === $data || false === $data->needsDynamicFields()) {
            return;
        }

        $form->add($this->factory->createNamed('choice', 'test', null, array(
            'choices' => array(1, 2, 3,),
            'expanded' => false,
            'label' => 'Test',
            'empty_value' => 'Select...',
        )));
    }
}

The form view from $form->createView() contains the elements that were added by the event subscriber. The prototype form view from form.properties.get('prototype') does not contain the elements elements that were added by the event subscriber.

Is there another supported method of getting an updated prototype in this scenario? If not - what would be the best way to customize to get this working?

Metadata

Metadata

Assignees

No one assigned

    Labels

    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.