sandeep singh
sandeep singh

Reputation: 127

How to make CAKEPHP radio button inside a label

I created following HTML code

<div class="btn-group" data-toggle="buttons">
    <label class="btn btn-primary active">
        <input type="radio" checked=""> Radio 1 
    </label>
    <label class="btn btn-primary">
        <input type="radio"> Radio 2
    </label>
</div>

Now i need this html with CAKEPHP form helper so try many mehtod to achieve this and finally i got a format which work partially not completely, I use the below code

$options=array('1'=>'Radio 1', '2'=> 'Radio 2');
$attributes=array(
    'legend'=>false,
    'label' => false,
    'div' => false,
    'class' => 'required',
    'default'=> 0,
    'before' => '<label class="btn btn-primary">',
    'separator' => '</label><label class="btn btn-primary">',
    'after' => '</label>',

);
echo $this->Form->radio('radio_btn',$options,$attributes);

But this code output look like below code

<div class="btn-group" data-toggle="buttons">
<input type="radio" name="data[RadioBtn][radio_btn]" id="RadioBtnId1" class="required" before="<label class=&quot;btn btn-primary&quot;>" after="</label>" value="1">Radio 1
<label class="btn btn-primary">
    <input type="radio" name="ata[RadioBtn][radio_btn]" id="RadioBtnId2" class="required" before="<label class=&quot;btn btn-primary&quot;>" after="</label>" value="2">Radio 2
</label>
</div>

Can anyone suggest me what is the problem on this code or if you have any idea,please help me.

Upvotes: 2

Views: 1619

Answers (1)

B. Feenstra
B. Feenstra

Reputation: 251

Little late to the party, but today I actually ran into the same issue. You can achieve this output by using the FormHelper::input(string $fieldName, array $options = array()) method:

$options = array(
    'type' => 'radio',
    'legend'=> false,
    'label' => false,
    'div' => array('class' => 'btn-group', 'data-toggle' => 'buttons'),
    'class' => 'required',
    'default'=> 0,
    'before' => '<label class="btn btn-primary">',
    'separator' => '</label><label class="btn btn-primary">',
    'after' => '</label>',
    'options' => array('1' => 'Radio 1', '2' => 'Radio 2'),
);
echo $this->Form->input('radio_btn', $options);

This results in the following HTML:

<div class="btn-group" data-toggle="buttons">
    <label class="btn btn-primary">
        <input type="radio" name="data[radio_btn]" id="radio_btn1" value="1" class="required">
        Radio 1
    </label>
    <label class="btn btn-primary">
        <input type="radio" name="data[radio_btn]" id="radio_btn2" value="2" class="required">
        Radio 2
    </label>
</div>

Upvotes: 1

Related Questions