Symfony 3嵌入表单标签而不是数字

时间:2018-05-16 12:45:55

标签: php symfony symfony-forms

是否可以在不使用javascript的情况下将这些数字(嵌入表单的名称)更改为其他标签(它们依赖于StatisticField的name属性)?

enter image description here

StatisticType具有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,

                    ]);

感谢。 的 [EDIT_1]: 我的模板:

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

2 个答案:

答案 0 :(得分:1)

尝试

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

这些数字来自CollectionType

编辑:

使用姓名

    $name = 'IamNotANumber';


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

答案 1 :(得分:0)

解决方案是覆盖StatisticFieldType(嵌入表单类型)中的buildView方法并从表单中获取数据:

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