Zend Form add span to label
Asked Answered
A

1

1

Zend driving me insane this afternoon, I need to add a inside the tag:

<label><span>Some Text</span></label>

Here is my Form decorators but it will not work for me at all it puts the before the first label:

 $checkbox->setDecorators(array(
            'ViewHelper',
            array(array('data' => 'HtmlTag'),
            array('tag' => 'div', 'class' => 'myspans')),
            array('Label', array('tag' => 'span', 'class' => 'sdfdsf'))
        ));

Any ideas its killing me!?

J

EDIT:

I am not sure if this effects it but its basically creating lots of checkboxes from an array, so its in a foreach loop:

        foreach($software as $prev => $p)
    {
        //$checkbox->setLabel($p);
        $checkbox->addMultiOption($prev,$p);
        $checkbox->setAttrib('id', 'checkbox');
        $checkbox->setSeparator('  ');
            $checkbox->setDecorators(array(
            'ViewHelper',
            array(array('data' => 'HtmlTag'),
            array('tag' => 'div', 'class' => 'myspans')),
            array('Label', array('tag' => 'span', 'class' => 'sdfdsf'))
        ));
    }
Amaranthaceous answered 20/9, 2013 at 14:27 Comment(0)
P
0

I could come up with two attempts:

1. Write a custom decorator:

class MyLabel extends Zend_Form_Decorator_Abstract {
        public function render($content) {
                return sprintf(
                    '<label><span>Some Text</span></label>%s',
                    $content
                ) ;
        }
}

And then simply add it to the list of decorators:

$checkbox->addDecorator(new MyLabel());

2. Disable escaping

You could also disable the escaping and directly place the html tag inside your label:

$checkbox->setLabel('<span>Some Text</span>');
$checkbox->setDecorators(array(
        'ViewHelper',
        array(array('data' => 'HtmlTag'),
        array('tag' => 'div', 'class' => 'myspans')),
        array('Label', array('escape' => false, 'tag' => 'span', 'class' => 'sdfdsf'))
    ));

Obvisouly this method should only be used with care as it could lead to XSS vulnerabilites.

Prebend answered 20/9, 2013 at 16:36 Comment(7)
Thanks for answer I need to wrap the span around the label output so it will be <span>label output</span>Amaranthaceous
The code above will give you <label><span>Some Text</span></label>. Isn't this what you wanted? If not, I'm afraid I misunderstood your question - In this case the best would be if you post an example how the whole form should look like in HTML.Prebend
Sorry for the confusion. I am outputting the form $this->formSoftware(); so when it outputs I need. <label><input type="checkbox"><span>label output</span>Amaranthaceous
But then you could use the custom decorator solution with return sprintf('<label>%s<span>Some Text</span></label>', $content);Prebend
Thanks how do I use the custom decorator and where do I put the code? In the form php? ThanksAmaranthaceous
The ZF docs have a good description on custom decorators and how to use them: framework.zend.com/manual/1.12/en/…Prebend
Thanks alot for your help appreciated.Amaranthaceous

© 2022 - 2024 — McMap. All rights reserved.