Symfony为选项entitytype元素提供类

时间:2017-10-04 20:14:28

标签: forms symfony

我有一个带字段的实体类型(nome,id,id_Categoria,id_tipo) id_tipo的值为1,2或3

  $builder->add('idTipologiaEsame', EntityType::class, array(
        'placeholder' => 'tipologia esame',
                'label' => false,
                'mapped' => false,
        'required' => false,

        'class' => 'AppBundle:Nome_esame',
                'choice_label' => 'nome',
                'group_by' => 'idCategoriaEsame.tipo',
            ))

如果id_tipo为1或2,我的目标是在select中为选项值提供一个类。

像这样:

  <select>
    <option class="ruminanti">val 1</option>  ->id_tipo=1
    <option class="pippo">val 2</option>      ->id_tipo=2
    <option class="ruminanti">val 3</option>  ->id_tipo=1
    </select>

有可能??

感谢RamazanApaydın我添加了这个

   $builder->add('idTipologiaEsame', EntityType::class, array(
            'placeholder' => 'tipologia esame',
                    'label' => false,
                    'mapped' => false,
            'required' => false,
                    'class' => 'AppBundle:Nome_esame',
                    'choice_label' => 'nome',
                    'group_by' => 'idCategoriaEsame.tipo',
             'choice_attr' => function($val, $key, $index) {
        // adds a class like attending_yes, attending_no, etc
            if($index){    ---->  i want id_tipo =1
            return ['class' => '.ruminati';}else{
                return ['class' => '.suini';

            }
    },

但我想添加条件id_tipo = 1

1 个答案:

答案 0 :(得分:3)

EntityType是ChoiceType元素。 Symfony文档中提供了一个示例。

https://symfony.com/doc/current/reference/forms/types/choice.html#choice-attr