How do I allow html tags in label for Zend form element using addElement()?
Asked Answered
L

2

7

I am brand new to Zend and I've been given a project to make adjustments on. I'd like to add html to the labels for my form elements but I can't seem to get it right.

Here's what I have:

$this->addElement('text', 'school_name', array(
    'filters' => array('StringTrim'),
        'validators' => array(
            array('StringLength', false, array(0, 150)),
        ),
    'required' => true,
    'label' => 'Name* :<img src="picture.png">,
    'size' => '90',
));

As is, of course, the <img src="picture.png"> text gets escaped and the whole string is displayed.

I've read that I need to use 'escape' => false in some capacity but I can't figure out where/how to use it in my specific case.

Any help would be great. Thanks!

Lipcombe answered 7/12, 2013 at 23:39 Comment(0)
S
9

After calling addElement fetch the label's decorator and change the escape setting:

$form->getElement('school_name')->getDecorator('label')->setOption('escape', false);

If you use this type of label a lot, you should consider writing a custom decorator.

Steen answered 8/12, 2013 at 8:42 Comment(4)
It didn't work. If I'm reading my debugger right (which I may not be), it looks like it's setting escape to false properly but it still is showing the full HTML on the page.Lipcombe
the code I used was $this->getElement('school_name')->getDecorator('label')->setOption('escape', false);Lipcombe
Ah, figured it out. I needed to put that code in the .phtml file and not in the constructor. Got it.Lipcombe
I was able to do it also before adding the elementAdverbial
P
7

You can also use the disable_html_escape in 'label_options' when adding an element to the form:

$this->add(array(
    ....
    'options' => array(
        'label' => '<span class="required">Name</span>,
        'label_options' => array(
             'disable_html_escape' => true,
         )
     ),
    ...
));

Credit to Théo Bouveret's post 'Button content in ZF2 forms' for the answer.

Puttyroot answered 15/1, 2015 at 15:17 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.