Php 如何在symfony表单选择中显示字体图标
我想用Symfony Form Builder在选择选项中显示所有字体的超级图标 我将选择字段添加为:Php 如何在symfony表单选择中显示字体图标,php,forms,symfony,font-awesome,Php,Forms,Symfony,Font Awesome,我想用Symfony Form Builder在选择选项中显示所有字体的超级图标 我将选择字段添加为: $choices = $this->getFontAwesome(); $form->add( $key, ChoiceType::class, array('label' => 'Texte', 'choices' => $choices, 'attr' => array('class' => "fa" ) ) ); 我的函数getFontAwesome
$choices = $this->getFontAwesome();
$form->add( $key, ChoiceType::class, array('label' => 'Texte', 'choices' => $choices, 'attr' => array('class' => "fa" ) ) );
我的函数getFontAwesome()
但在选择字段中,看不到图标:
字段显示代码而不是图标
我该怎么办?
我尝试了htmlspecialschars和其他(htmlentities,…)但是不起作用。如果你没有使用任何像或这样的js插件,那么你有这种可能性,但我们需要做一些工作才能达到它 首先,使用
作为标签不是一种选择,因为
元素不能有任何子元素,只能有文本。()
为了可重用性,让我们构建一个名为“IconChoiceType”的表单类型作为“ChoiceType”的子类型:
到目前为止,没有什么结果和你的不同
<select id="form_icon" name="form[icon]" style="font-family: 'FontAwesome';">
<option value="fa-glass"></option>
<option value="fa-music"></option>
...
</select>
请注意,{{choice.label | raw}}
显示存储在标签中的原始文本(防止转义),在本例中为图标字体内容
最后,您需要注册表单主题,如描述:
结论:
$form->add('icon', IconChoiceType::class);
您正在为此使用select2插件吗?
# app/config/service.yml
services:
app.form.icon_choice_type:
class: AppBundle\Form\Type\ChoiceIconType
# Symfony has already a container parameter to the kernel root directory.
arguments: ['%kernel.root_dir%']
tags:
- { name: form.type }
<select id="form_icon" name="form[icon]" style="font-family: 'FontAwesome';">
<option value="fa-glass"></option>
<option value="fa-music"></option>
...
</select>
{# app/Resources/views/form/fields.html.twig #}
{#
here isn't need to create the expected `icon_choice_widget` like shown
the documentation, because this looks equal to `choice_widget` from
`ChoiceType`, only we need overwrite the block that renders the label.
#}
{%- block choice_widget_options -%}
{% for group_label, choice in options %}
{%- if choice is iterable -%}
<optgroup label="{{ choice_translation_domain is same as(false) ? group_label : group_label|trans({}, choice_translation_domain) }}">
{% set options = choice %}
{{- block('choice_widget_options') -}}
</optgroup>
{%- else -%}
{# this line has been overwritten, see {{- block('choice_option_label') -}} to end #}
<option value="{{ choice.value }}"{% if choice.attr %} {% set attr = choice.attr %}{{ block('attributes') }}{% endif %}{% if choice is selectedchoice(value) %} selected="selected"{% endif %}>{{- block('choice_option_label') -}}</option>
{%- endif -%}
{% endfor %}
{%- endblock choice_widget_options -%}
{%- block choice_option_label -%}
{# this block has been called from choice_widget_options block #}
{%- if raw_label|default(false) -%}
{# the label is rendered as raw when IconChoiceType is used #}
{{ choice_translation_domain is same as(false) ? choice.label|raw : choice.label|trans({}, choice_translation_domain)|raw }}
{%- else -%}
{{ choice_translation_domain is same as(false) ? choice.label : choice.label|trans({}, choice_translation_domain) }}
{%- endif -%}
{%- endblock -%}
# app/config/config.yml
{# ... #}
twig:
form_themes:
- 'form/fields.html.twig'
$form->add('icon', IconChoiceType::class);