Zend_form_element_radio 无法将类添加到标签

发布于 2024-12-08 07:22:34 字数 1309 浏览 0 评论 0原文

我试图向一组单选按钮中的每个标签添加一个类(相同)。

这是我的代码:

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

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

非常欢迎想法、解决方案、建议!提前致谢!

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

雨落□心尘 2024-12-15 07:22:34

我花了两个小时才找到答案,而且答案非常简单!将此行添加到您的声明中:-

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

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

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.

东走西顾 2024-12-15 07:22:34

试试这个:

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

或者

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

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' . '"/>');
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文