Zend_form_element_radio无法将类添加到标签

时间:2011-10-05 13:05:10

标签: php zend-framework radio-button label zend-form

我正在尝试为一组单选按钮中的每个标签添加一个类(相同)。

这是我的代码:

$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');

这是我的输出:

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

这是我想要的输出:

<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">

愚蠢的是它到目前为止适用于我所有其他表单元素。我尝试了一百万种组合并搜索了我的屁股,但无论我尝试什么,我都无法在标签上添加类。

我们非常欢迎您的想法,解决方案和建议!提前谢谢!

2 个答案:

答案 0 :(得分:18)

我花了两个小时才找到答案,这真是太简单了!将此行添加到您的声明中: -

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

Zend / View / Helper / FormRadio.php第79-95行给了我线索。

   $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]);
        }
    }

这适用于我的系统,我希望它也适合你。

答案 1 :(得分:0)

试试这个:

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