订购的其他选择字段

时间:2019-04-01 21:23:51

标签: forms symfony

我想设置新条目的顺序。我需要先定位并在现有条目之一之后排序

我不知道如何在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>

2 个答案:

答案 0 :(得分:0)

如果您想在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)< / p>

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

答案 1 :(得分:0)

如果要在选项中添加自定义文本,则可以使用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()));
}

有了这个,您将获得预期的结果。

您可以将特定的吸气剂添加到ProductType实体类中,然后直接在choice_label中使用它,就像这样:

ProductType.php

class ProductType {
    ...

    getNameDeWithCustomText() {
        return 'after ' . $this->getNameDe();
    }
}
'choice_label' => 'nameDeWithCustomText',