2
votes

I want to use Radio Button using the Form Helper. A radio button has a Radio element and a Label. I have by default Display: block for Label element. I want to assign a class the the label of the Radio button so that i can assign it a inline-block.

$attributes  = array('legend' => false, 'label' => array('class' => 'radioBtn'));
echo $this->Form->radio('gender', $options, $attributes);

How can i assign a class to the label of the option

3

3 Answers

5
votes

By looking at the Form->radio() method code, nothing seems related to attributes belonging to the labels.

But to modify the display of these labels, you could use a surrounding div

echo '<div class="inline_labels">';
echo $this->Form->radio('gender', $options, $attributes);
echo '</div>';

and use CSS like this:

.inline_labels label
{
    display: inline-block;
}
1
votes

How about using FormHelper::label(string $fieldName, string $text, array $options) You could define the label class in the options array, so (for example):

echo $options = array( /* relevant stuff goes here */ );
echo $attributes = array( /* relevant stuff goes here */ );
echo $this->Form->radio('gender', $options, $attributes);
echo $this->Form->label('gender', 'Text of your label', array('label'=>'radioBtn'))

Source CakePHP Cookbook on FormHelper

1
votes

Work for me:

//  Add your own label with CSS class
$opts = array('1' => "<label class='myCSS'>My first option</label>", "2" => "<label class='myCSS'>My second option</label>");

//  Put label param to false
echo $this->Form->input('my-input', array('type' => 'radio', 'label' => false, 'options' => $opts, 'legend' => false));

Enjoy