Cakephp Form Radio Button Label Class

12,758

Solution 1

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;
}

Solution 2

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

Solution 3

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

Share:
12,758
Harsha M V
Author by

Harsha M V

I turn ideas into companies. Specifically, I like to solve big problems that can positively impact millions of people through software. I am currently focusing all of my time on my company, Skreem, where we are disrupting the ways marketers can leverage micro-influencers to tell the Brand’s stories to their audience. People do not buy goods and services. They buy relations, stories, and magic. Introducing technology with the power of human voice to maximize your brand communication. Follow me on Twitter: @harshamv You can contact me at -- harsha [at] skreem [dot] io

Updated on June 04, 2022

Comments

  • Harsha M V
    Harsha M V almost 2 years

    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