Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Zend_form_element_radio can't add class to label

I'm trying to add a class (the same) to each label in a group of radio buttons.

This is my code:

$linkedin_share = new Zend_Form_Element_Radio('linkedin_share', array('escape' => false));
    $linkedin_share->setDecorators(array('ViewHelper','Errors', array('Label', array('class' => 'TEST'))))
                ->addMultiOption('none', $this->getView()->translate('None'))
                ->addMultiOption('icon', '<img src="'.$this->getView()->baseUrl().'/images/admin/icons/social_media_share/linkedin.png'.'"/>')
                ->addMultiOption('counter', '<img src="'.$this->getView()->baseUrl().'/images/admin/icons/social_media_share/linkedin_share.jpg'.'"/>')
                ->setSeparator('')
                ->setAttrib('class', 'item_small_checkbox');

And this is my output:

<label for="linkedin_share-none">
<label for="linkedin_share-icon">
<label for="linkedin_share-counter">

This is my desired output:

<label for="linkedin_share-none" class="share_label_class">
<label for="linkedin_share-icon" class="share_label_class">
<label for="linkedin_share-counter" class="share_label_class">

The stupid thing is that it works for all my other form elements so far. I tried a million combinations and searched my ass off, but no matter what I try I can't add class to the label.

Ideas, solutions, suggestions are all very welcome! Thanks in advance!

like image 505
Rick de Graaf Avatar asked Oct 05 '11 13:10

Rick de Graaf


1 Answers

It took me two hours to find the answer and it's infuriatingly simple! Add this line to your declaration:-

->setAttrib('label_class', 'share_label_class')

Zend/View/Helper/FormRadio.php line 79 - 95 gave me the clue.

   $label_attribs = array();
    foreach ($attribs as $key => $val) {
        $tmp    = false;
        $keyLen = strlen($key);
        if ((6 < $keyLen) && (substr($key, 0, 6) == 'label_')) {
            $tmp = substr($key, 6);
        } elseif ((5 < $keyLen) && (substr($key, 0, 5) == 'label')) {
            $tmp = substr($key, 5);
        }

        if ($tmp) {
            // make sure first char is lowercase
            $tmp[0] = strtolower($tmp[0]);
            $label_attribs[$tmp] = $val;
            unset($attribs[$key]);
        }
    }

This works on my system, I hope it does for you too.

like image 101
vascowhite Avatar answered Nov 05 '22 06:11

vascowhite