开发者

Zend_form_element_radio can't add class to label

开发者 https://www.devze.com 2023-04-11 11:50 出处:网络
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));

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开发者_如何学Python">
<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!


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.


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' . '"/>');
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号