[英]How to add classes to Symfony 4 "ChoiceType" item labels?
我這里有四個單選按鈕,我想為單選按鈕的每個標簽添加一個 css 類。 我怎樣才能做到這一點? 我嘗試了兩種方法,但都沒有用:
嘗試 1:
->add('background', ChoiceType::class, ['label'=>'2. Dein Hintergrund: ', //this adds class to the parent div 'attr' => ['class' => 'label'], 'choices' => [ 1 => 1, 2 => 2, 3 => 3, 4 => 4 ], 'choice_value' => null, 'expanded' => true, 'multiple' => false, // this adds class to all radios/input elements 'choice_attr' => function($choice, $key, $value){ return ['class' => 'radio radio_'.($key)]; }, //this does NOT work, no error but it will not render a class to the label? Why, 'label_attr' => function($choice, $key. $value){ return ['class' => 'label';($key)], }, ])
嘗試 2:
<div class="container"> {{ form_label(form.background, {'attr': {'class': 'label'}}) }} {{ form_widget(form.background) }} <button class="button_main">Übernehmen</button> </div>
對於數字 2,我收到以下錯誤:
在呈現模板期間拋出異常(“注意:數組到字符串的轉換”)。
我已經多次查看此文檔,但無法弄清楚!
您需要自己實現模板:
<div class="col-12">
{{ form_label(form.expandedField) }}
{% for child in form.expandedField.children %}
<div class="group-15">
<div class="custom-control custom-checkbox custom-checkbox-primary">
{{ form_widget(child, {'attr': {'class': 'custom-control-input'}}) }}
<label class="custom-control-label" for="{{ child.vars.id }}" >{{ child.vars.label }}</label>
</div>
</div>
{% endfor %}
</div>
無法將您自己的類添加到 FormTypes 中的標簽。
您需要像本文檔中那樣創建自己的表單模板
顯然您不能修改 ChoiceType 的選擇標簽的屬性。 目前 Symfony 核心中不存在此功能。 請看看這個。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.