我想设置新条目的顺序。我需要订购首先在现有的一个条目之后定位和订购
我不知道如何在Symfony 4.2中管理它
public function buildForm(FormBuilderInterface $builder, array $options) {
$builder
->add('nameDe')
->add('nameEn')
->add('descriptionDe')
->add('descriptionEn')
->add('rank', EntityType::class, [
'class' => ProductType::class,
'query_builder' => function (EntityRepository $er) {
return $er->createQueryBuilder('p')
->orderBy('p.rank', 'ASC');
},
'choice_label' => 'nameDe',
])
->add('active')
->add('creatDate')
->add('updateDate', DateTimeType::class, array('data' => new \DateTime()));
}
我有这个:
<select id="product_type_rank" name="product_type[rank]">
<option value="1">Internetseiten</option>
<option value="2">Printmedia</option>
</select>
我需要这样的东西
<select id="product_type_rank" name="product_type[rank]">
<option value="0">At the beginning</option>
<option value="1">after Internetseiten</option>
<option value="2">after Printmedia</option>
</select>
如果要在选项中添加自定义文本,可以使用choice_label
选项:
public function buildForm(FormBuilderInterface $builder, array $options) {
$builder
->add('nameDe')
->add('nameEn')
->add('descriptionDe')
->add('descriptionEn')
->add('rank', EntityType::class, [
'class' => ProductType::class,
'query_builder' => function (EntityRepository $er) {
return $er->createQueryBuilder('p')
->orderBy('p.rank', 'ASC');
},
'choice_label' => function($productType) {
return 'after ' . $productType->getNameDe();
},
'placeholder' => 'At the beginning',
])
->add('active')
->add('creatDate')
->add('updateDate', DateTimeType::class, array('data' => new \DateTime()));
}
有了这个,你将获得预期的结果。
您可以将特定的getter添加到ProductType实体类中,并直接在choice_label
中使用它,如下所示:
ProductType.php
class ProductType {
...
getNameDeWithCustomText() {
return 'after ' . $this->getNameDe();
}
}
'choice_label' => 'nameDeWithCustomText',
如果要在EntityType字段中添加默认选项,可以添加这样的占位符选项
public function buildForm(FormBuilderInterface $builder, array $options) {
$builder
->add('nameDe')
->add('nameEn')
->add('descriptionDe')
->add('descriptionEn')
->add('rank', EntityType::class, [
'class' => ProductType::class,
'query_builder' => function (EntityRepository $er) {
return $er->createQueryBuilder('p')
->orderBy('p.rank', 'ASC');
},
'choice_label' => 'nameDe',
'placeholder' => 'At the beginning',
])
->add('active')
->add('creatDate')
->add('updateDate', DateTimeType::class, array('data' => new \DateTime()));
}
正如我所看到的,您正在处理表单中的多种语言,如果要将占位符国际化,只需将翻译键直接放在值中(您需要安装Symfony翻译包:https://symfony.com/doc/current/translation.html)
public function buildForm(FormBuilderInterface $builder, array $options) {
$builder
->add('nameDe')
->add('nameEn')
->add('descriptionDe')
->add('descriptionEn')
->add('rank', EntityType::class, [
'class' => ProductType::class,
'query_builder' => function (EntityRepository $er) {
return $er->createQueryBuilder('p')
->orderBy('p.rank', 'ASC');
},
'choice_label' => 'nameDe',
'placeholder' => 'foo.bar.placeholder',
])
->add('active')
->add('creatDate')
->add('updateDate', DateTimeType::class, array('data' => new \DateTime()));
}
如果翻译文件不存在,请在translations/
文件夹中创建它们
# translations/messages.en.yaml
foo:
bar:
placeholder: At the beginning
# translations/messages.de.yaml
foo:
bar:
placeholder: Am Anfang