Search code examples
phpyii2radio-button

How to customise outer div when creating radioList in Yii framework 2?


Below is my bootstrap activefield that generates radiolist.

<?= $form->field($model, 'photo_id')->radioList(['A', 'B', 'C']) ?>

The above code generates the following HTML tags.

<div id="model-photo_id">
    <div class="radio">
        <label>
            <input type="radio" name="Model[photo_id]" value="0" checked=""> 
            A
        </label>
    </div>
    <div class="radio">
        <label>
            <input type="radio" name="Model[photo_id]" value="1" checked=""> 
            B
        </label>
    </div>
    <div class="radio">
        <label>
            <input type="radio" name="Model[photo_id]" value="2" checked=""> 
            C
        </label>
    </div>
</div>

The target is <div class="radio">. I would like to customise this div by changing the class name or adding more class name, adding more attributes into this div, ect. How can I do that?


Solution

  • You can try with this custom template, instead of applying class to radio div, you can apply class to label also

    echo $form->field($model, 'photo_id')
            ->radioList(
                    [0 => 'A', 1 => 'B', 2 => 'C'], [
                'item' => function($index, $label, $name, $checked, $value) {
    
                    $return = '<label class="modal-radio">';
                    $return .= '<input type="radio" name="' . $name . '" value="' . $value . '" tabindex="3">';
                    $return .= '<i></i>';
                    $return .= '<span>' . ucwords($label) . '</span>';
                    $return .= '</label>';
    
                    return $return;
                }
                    ]
            )
            ->label(false);