Home > Software design >  Icons in a Symfony ChoiceType
Icons in a Symfony ChoiceType

Time:03-14

So i have a Symfony Form and i try to display a SelectField with different Icons.

My ChoiceType looks like this

  ->add('icon', ChoiceType::class, [
            'choices'  => [
                '' => '',
                '' => '',
            ],
            'mapped' => false,
        ])

My CSS looks like this

select { font-family: 'FontAwesome', Verdana }

So when i render a normal select div it works very well and i can see the icons. But when i try it via the Symfony Form it does not work. I have already read other Threads with the same problem. One solution was the to set the autoescape of twig to false and try to work with the raw tag. But still i just see the pure text "" and not the Icons in the Select Box.

Anyone have a suggestion?

Kind Regards

Magnus

CodePudding user response:

One option, you can do this using the html_entity_decode function

public function buildForm(FormBuilderInterface $builder, array $options)
{
    // prepare an array with icons
    $icons =  [
        '',
        '',
    ];
    
    // decode our icons
    $icons = array_flip(array_map('html_entity_decode',$icons));
    
    // add field to formBuilder
    $builder->add('icon', ChoiceType::class, [
        'choices'  => $icons,
        'mapped' => false,
    ]);

}

add css

select { font-family: 'FontAwesome'}
  • Related