Zend_form_element_radio can't add class to label
Asked Answered
B

2

6

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!

Bowhead answered 5/10, 2011 at 13:5 Comment(1)
Could you give us the code of the elements for wich it works ? I would try to look at what's different in between those, or what's different in the form of the elements that are working (if it's not the same).Pullen
P
18

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.

Peres answered 6/10, 2011 at 1:9 Comment(2)
Wow! So incredibly simple! Thanks a lot! It's a bummer that ZF really lacks a lot in documentation at some points.Bowhead
Yeah, the best documentation is the code. It's well written, so you can usually work things out eventually. Thanks for the interesting question.Peres
P
0

try this:

$linkedin_share = new Zend_Form_Element_Radio('linkedin_share', array('escape' => false));
$linkedin_share->setAttrib('class', 'item_small_checkbox');
    $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('');

OR

//linkedin_share

$this->addElement(
    'radio',
    'linkedin_share',
    array(
         'label' => 'Linkedin Share',
         'separator' => ' ',
         'class' => 'item_small_checkbox',
    )
);
$this->linkedin_share->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' . '"/>');
Politicking answered 5/10, 2011 at 13:17 Comment(3)
Doesn't work. I already tried applying the decorators in different positions.Bowhead
How do you mean? Remove all the other decorators? That didn't work either. I also tried adding all possible decorators.Bowhead
let us continue this discussion in chatPoliticking

© 2022 - 2024 — McMap. All rights reserved.