Symfony (Silex) radio choice form builder does not render attr

风格不统一 提交于 2019-12-12 02:55:56

问题


I'm trying to add attributes to my rendered radios

$builder
    ->add('myRadios', 'choice', array(
        'choices' => array(
            'one' => 'uno',
            'two' => 'due'),
        'multiple' => false,
        'attr' => array('class' => 'testClass'),
        'expanded' => true

the output is:

<div class="control-group">
    <label class="control-label required">Myradios</label>
    <div class="controls">
        <label for="form_one_0" class="required radio">
            <input type="radio" id="form_one_0" name="form[one]" required="required" value="uno" />
            <span>Uno</span>
        </label>
        <label for="form_two_1" class="required radio">
            <input type="radio" id="form_two_1" name="form[two]" required="required" value="due" />
            <span>Due</span>
        </label>
    </div>
</div>

no references to class='testClass'

I can't find any issue online


回答1:


Try this way Adam,

$form = $app['form.factory']->createBuilder('form')
        ->add('myRadios', 'choice', array(
            'choices' => array(
                'one' => 'uno',
                'two' => 'due'),
            'multiple' => false,
            'expanded' => true,
            'attr' => array('class' => 'testClass'),
        ))
        ->getForm();

it works:

<div id="form_myRadios" class="testClass">
     <input type="radio" id="form_myRadios_0" name="form[myRadios]" required="required" value="one">
     <label for="form_myRadios_0" class="required">uno</label>
     <input type="radio" id="form_myRadios_1" name="form[myRadios]" required="required" value="two">
     <label for="form_myRadios_1" class="required">due</label>
</div>



回答2:


How does your twig code look like? And which form template do you use?

You use a custom form template because <div class="control-group"> <label class="control-label required">Myradios</label> <div class="controls"> is definitely not standard Symfony!



来源:https://stackoverflow.com/questions/16571259/symfony-silex-radio-choice-form-builder-does-not-render-attr

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!