aufziehvogel
aufziehvogel

Reputation: 7297

How can I use HTML tags in a Zend\Form placeholder attribute

I use Zend\Form to generate my forms. I have a textarea where I want to display some default text. Currently, I have it like this:

$this->add(array(                                                        
    'type' => 'textarea',                                                
    'name' => 'dialog',
    'options' => array(                                                  
        'label' => 'Dialog',                                             
    ),
    'attributes' => array(
        'placeholder' => 'Person1: Hello!<br />Person2: Hi!',                                               
    ),                                                                   
));

However, this results in <br /> being escaped to &lt;br /&gt;.

Is there a way to embed HTML tags in the placeholder through Zend\Form?

Upvotes: 0

Views: 1370

Answers (2)

jsteinmann
jsteinmann

Reputation: 4752

for reference, in ZF1 you could create an element with default text in a textarea:

    $this->createElement(
        'textarea',
        'content', 
        array(
            'label' => 'Address', 
            'rows' => '3', 
            'value' => 'default value'
        )
    );

For ZF2, I recommend taking a look at the following documentation:

textarea docs

the library component you are using with for elements is,

class Element implements
    ElementAttributeRemovalInterface,
    ElementInterface,
    InitializableInterface

which is found at \Zend\Form\Element in the library. you can see in the class that there are several methods for customizing your form elements which includes ::setValue() that you can use to set a value on a form element.

I believe this is was you are looking for, i.e. you don't need to use html tags in the placeholder attribute if what you are trying to do is simply have default text.

Upvotes: -1

Sam
Sam

Reputation: 16455

You can't and you shouldn't. Please see the official specifications:

The placeholder attribute represents a short hint (a word or short phrase) intended to aid the user with data entry when the control has no value. A hint could be a sample value or a brief description of the expected format. The attribute, if specified, must have a value that contains no U+000A LINE FEED (LF) or U+000D CARRIAGE RETURN (CR) characters.

If you absolutely have to mimic this behavior, do so via JavaScript.

Upvotes: 3

Related Questions