Symfony 3+:设置字段的父div(包含标签和输入的div)的样式

kzmpq1sx  于 2022-11-16  发布在  其他
关注(0)|答案(2)|浏览(143)

首先,我的目标是使两个输入字段内联,图片是附件:

Symfony允许我们通过对字段的form_widget使用attr和对form_label使用label_attr来设置表单字段的样式,但在这里我想对div应用一个样式,该div包含Symfony在分支中自动生成的label + input:

$builder
->add('language', ChoiceType::class, array(
    'choices'  => array(
        'FR' => 5,
        'EN' => 1,
     ),
    'label'      => ' ',
    'attr' => array('style' => 'float:left;width:40%'),
))
->add('level', ChoiceType::class, array(
    'choices'  => array(
        'Good' => 5,
        'Bad' => 1,
     ),
    'label'      => ' ',
    'attr' => array('style' => 'float:left;width:40%'),
));

result:样式已经应用到输入(在我们的例子中是select),div父项没有受到影响(保持空样式):

<div id="homebundle_personnel_personnelLanguages_0">
   <div> <!-- the div that i want to apply a style -->
      <label for="homebundle_personnel_personnelLanguages_0_language" class="required"> </label>
      <select id="homebundle_personnel_personnelLanguages_0_language" name="homebundle_personnel[personnelLanguages][0][language]" style="float:left;width:40%">
         <option value="5">FR</option>
         <option value="1">EN</option>
      </select>
   </div>
   <div>
      <label for="homebundle_personnel_personnelLanguages_0_level" class="required"> </label>
      <select id="homebundle_personnel_personnelLanguages_0_level" name="homebundle_personnel[personnelLanguages][0][level]" style="float:left;width:40%">
         <option value="5">Good</option>
         <option value="1">Bad</option>
      </select>
   </div>
</div>
a1o7rhls

a1o7rhls1#

下定决心,我必须在小枝的层面上对待这一点:

<div style="float:left;width:40%" >
    {{ form_widget(form.language) }}
</div>

<div style="float:left;width:40%" >
    {{ form_widget(form.level) }}
</div>
c2e8gylq

c2e8gylq2#

用途:

'row_attr' => [
    'class' => 'yourClass',
],

相关问题