placeholder text in zend text element

2019-04-06 05:24发布

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 ?

5条回答
小情绪 Triste *
2楼-- · 2019-04-06 05:28

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 !

查看更多
不美不萌又怎样
3楼-- · 2019-04-06 05:30

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'));
查看更多
家丑人穷心不美
4楼-- · 2019-04-06 05:37

My solution using jQuery

var elements = ['email', 'password', 'password_confirm'];
$j(elements).each(function(i, elem){
    $j('#' + elem).attr('placeholder', $j('#' + elem + '-label label').text());
    $j('#' + elem + '-label').hide();
    if ($j('#' + elem).hasClass('required-entry')) $j('#' + elem).attr('placeholder', $j('#' + elem).attr('placeholder') + ' *');
});

查看更多
我只想做你的唯一
5楼-- · 2019-04-06 05:47

On Zend_Form_Element objects you can specify attributes:

$element->setAttrib('placeholder', 'Search');
查看更多
在下西门庆
6楼-- · 2019-04-06 05:48

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

    $element->setAttrib ( 'placeholder', 'search' );
查看更多
登录 后发表回答