Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

placeholder text in zend text element

I am working on a Zend form application where my form contains text boxes with watermarks.

we can achieve this in HTML by the following code:

<input type="text" placeholder="Search" name="q" />

My question is how to add the placeholder attribute in my input-box using Zend form ?

like image 995
Amritpal singh Avatar asked Jul 04 '11 15:07

Amritpal singh


4 Answers

It's already been mentioned to use:

$element->setAttrib('placeholder', 'Search');

You can also use it like this when extending Zend_Form

$element = $this->createElement('text', 'q', array(
           'placeholder' => 'Search',
           'label'       => 'Search'
));

Or inside the view using Zend_View_Helper_FormText

echo $this->formText('q',null, array('placeholder' => 'Search'));
like image 159
brady.vitrano Avatar answered Oct 07 '22 23:10

brady.vitrano


I think you can call settAttrib() on your element like this when you define elements

    $element->setAttrib ( 'placeholder', 'search' );
like image 20
Nicola Peluchetti Avatar answered Oct 07 '22 23:10

Nicola Peluchetti


On Zend_Form_Element objects you can specify attributes:

$element->setAttrib('placeholder', 'Search');
like image 33
Richard Parnaby-King Avatar answered Oct 08 '22 01:10

Richard Parnaby-King


Here's an update for ZF2.
You'll have to use this in your Zend\Form\Form :

$this->add(
    [
        'name'    => 'q',
        'type'    => 'Text',
        'options' => [
            'label' => 'Search',
        ],
        'attributes' => [
            'placeholder' => 'Search',
        ],
    ]
);

setAttrib doesn't exists, but setAttribute does :

$element->setAttribute('placeholder', 'Search');

But in FormText view-helper, you can't add options anymore, so you have to do :

$element = $form->get('q');
$saved_placeholder = $element->getAttribute('placeholder'); // works even if not defined
$element->setAttribute('placeholder', 'Search');
echo $this->formText($element);
$element->setAttribute('placeholder', $saved_placeholder);

I know, this is an ugly hack !

like image 28
Stopi Avatar answered Oct 08 '22 00:10

Stopi