Symfony 3 嵌入式表格标签不是数字
Symfony 3 embedded forms labels not numbers
是否可以在不使用 javascript 的情况下将这些数字(嵌入表单的名称)更改为其他一些标签(它们取决于 StatisticField 的名称 属性)?
StatisticType 有 StatisticFieldTypes:
$builder->add('statisticFields', CollectionType::class, array(
'entry_type' => StatisticFieldType::class,
'by_reference' => true,
));
统计字段类型:
$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) }}
尝试
$builder->add('statisticFields', CollectionType::class, array(
'entry_type' => StatisticFieldType::class,
'entry_options' => array('label' => false),
'by_reference' => true,
));
数字来自CollectionType
编辑:
有名字
$name = 'IamNotANumber';
$builder->add('statisticFields', CollectionType::class, array(
'entry_type' => StatisticFieldType::class,
'entry_options' => array('label' => $name),
'by_reference' => true,
));
解决方案是覆盖 StatisticFieldType(嵌入式表单类型)中的 buildView 方法并从表单中获取数据:
/**
* {@inheritDoc}
*/
public function buildView(FormView $view, FormInterface $form, array $options)
{
// For Symfony 2.1 and higher:
$view->vars['label'] = $form->getData()->getName();
}
是否可以在不使用 javascript 的情况下将这些数字(嵌入表单的名称)更改为其他一些标签(它们取决于 StatisticField 的名称 属性)?
StatisticType 有 StatisticFieldTypes:
$builder->add('statisticFields', CollectionType::class, array(
'entry_type' => StatisticFieldType::class,
'by_reference' => true,
));
统计字段类型:
$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) }}
尝试
$builder->add('statisticFields', CollectionType::class, array(
'entry_type' => StatisticFieldType::class,
'entry_options' => array('label' => false),
'by_reference' => true,
));
数字来自CollectionType
编辑:
有名字
$name = 'IamNotANumber';
$builder->add('statisticFields', CollectionType::class, array(
'entry_type' => StatisticFieldType::class,
'entry_options' => array('label' => $name),
'by_reference' => true,
));
解决方案是覆盖 StatisticFieldType(嵌入式表单类型)中的 buildView 方法并从表单中获取数据:
/**
* {@inheritDoc}
*/
public function buildView(FormView $view, FormInterface $form, array $options)
{
// For Symfony 2.1 and higher:
$view->vars['label'] = $form->getData()->getName();
}