Symfony ChoiceType 中的图标

发布于 2025-01-13 14:57:35 字数 579 浏览 3 评论 0原文

我有一个 Symfony 表单,我尝试用不同的图标显示 SelectField

我的 ChoiceType 看起来像这样

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

我的 CSS 看起来像这样

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

当我渲染一个普通的选择 div 时,它工作得很好,我可以看到图标。

但是当通过 Symfony Form 尝试它时,它不起作用。我已经读过其他有同样问题的主题。一种解决方案是将 twig 的自动转义设置为 false 并尝试使用原始标签。但我仍然只看到纯文本“”,而不是选择框中的图标。

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 }

When I render a normal select div it works very well and i can see the icons.

But when 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 auto-escape 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

別甾虛僞 2025-01-20 14:57:35

一种选择是,您可以使用 html_entity_decode 函数

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,
    ]);

}

添加 css来执行此操作

select { font-family: 'FontAwesome'}

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'}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文