Search code examples
phpsymfonysymfony-forms

Symfony 3 embedded forms labels not numbers


Is it possible to change those numbers (names of embedded forms) to some other labels (they depend on StatisticField's name property) without using javascript?

enter image description here

StatisticType has StatisticFieldTypes:

$builder->add('statisticFields', CollectionType::class, array(
            'entry_type' => StatisticFieldType::class,
            'by_reference' => true,
        ));

StatisticFieldType:

 $builder->add('dateSince', DateTimeType::class, [
                    'widget' => 'single_text',
                    'label' => 'date.since',
                    'required' => false,

                ])
                    ->add('dateTo', DateTimeType::class, [
                        'widget' => 'single_text',
                        'label' => 'date.to',
                        'required' => false,

                    ]);

Thanks. [EDIT_1]: My template:

<h1>Statistic creation</h1>
{{ form_start(form) }}
{{ form_widget(form) }}
<input type="submit" value="Create"/>
{{ form_end(form) }}

Solution

  • The solution was to override buildView method in StatisticFieldType (embedded form type) and get the data from the form :

       /**
         * {@inheritDoc}
         */
        public function buildView(FormView $view, FormInterface $form, array $options)
        {
            // For Symfony 2.1 and higher:
            $view->vars['label'] = $form->getData()->getName();
        }