翻译结果:从Symfony表单中为ChoiceType元素选择样式,你好各位。

huangapple go评论72阅读模式
英文:

Styling ChoiceType element from symfony forms Hello guys

问题

你好。

你可以在Twig文件中为ChoiceType的选择框添加样式。你需要获取ChoiceType的每个选择框,并为它们添加样式。以下是一个示例代码,演示如何在Twig模板中获取并样式化这些选择框:

{% for role, label in form.roles.vars.choices %}
    <div class="form-check">
        {{ form_widget(form.roles[role], {'attr': {'class': 'your-custom-class'}}) }}
        {{ form_label(form.roles[role]) }}
    </div>
{% endfor %}

在这个示例中,我们遍历了ChoiceType的选择项,为每个选择项创建了一个包含自定义类的form-check元素。你可以将你的自定义类替换为所需的样式类。

希望这可以帮助你样式化Twig模板中的选择框。如果有任何问题,请随时提问。不用担心英文,我会尽力协助你。

英文:

Hillo.
I hawe a UserType form:

-&gt;add(&#39;roles&#39;, ChoiceType::class, [
                &#39;choices&#39; =&gt; [&#39;ROLE_ADMIN&#39; =&gt; &#39;ROLE_ADMIN&#39;,
                    &#39;ROLE_MANAGER&#39; =&gt; &#39;ROLE_MANAGER&#39;,
                    &#39;ROLE_CUSTOMER&#39; =&gt; &#39;ROLE_CUSTOMER&#39;
                ],
                &#39;choice_attr&#39; =&gt; [
                    &#39;ROLE_ADMIN&#39; =&gt; [&#39;class&#39; =&gt; &#39;form-check-input&#39;],
                    &#39;ROLE_MANAGER&#39; =&gt; [&#39;class&#39; =&gt; &#39;form-check-input&#39;],
                    &#39;ROLE_CUSTOMER&#39; =&gt; [&#39;class&#39; =&gt; &#39;form-check-input&#39;],
                ],
                &#39;expanded&#39; =&gt; true,
                &#39;multiple&#39; =&gt; true,
            ])

I wont to styling roles inputs in twig file.

I try add choise atribute

I need get every select from ChoiceType in twig tamplate and styling it.
How can i do this?
P. S.
Sorry for my bed english

答案1

得分: 0

为了在Twig中为ChoiceType字段样式化选择项,您可以在模板中迭代选择项并应用所需的样式。

{% for choice in form.roles %}
    <div class="form-check">
        {{ form_widget(choice, {'attr': {'class': 'form-check-input'}}) }}
        {{ form_label(choice) }}
    </div>
{% endfor %}

Inform.roles是ChoiceType字段。通过迭代form.roles,您可以逐个访问每个选择项。form_widget(choice)渲染输入元素,form_label(choice)渲染每个选择项的标签。attr参数用于添加所需的样式类以进行样式化。

请确保在Twig模板或链接的CSS文件中包括必要的CSS类和样式,以实现所需的视觉样式。

英文:

To style the choices in your ChoiceType field in Twig, you can iterate over the choices in your template and apply the desired styling.

{% for choice in form.roles %}
    &lt;div class=&quot;form-check&quot;&gt;
        {{ form_widget(choice, {&#39;attr&#39;: {&#39;class&#39;: &#39;form-check-input&#39;}}) }}
        {{ form_label(choice) }}
    &lt;/div&gt;
{% endfor %}

Inform.roles is the ChoiceType field. By iterating over form.roles, you can access each choice individually. form_widget(choice) renders the input element, and form_label(choice) renders the label for each choice. The attr parameter is used to add the desired class for styling purposes.

Make sure to include the necessary CSS classes and styles in your Twig template or linked CSS file to achieve the desired visual styling.

huangapple
  • 本文由 发表于 2023年6月1日 00:35:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/76375648.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定