how to add special class for labels and errors on zend form elements?

时光怂恿深爱的人放手 提交于 2020-01-15 09:17:09

问题


how we could add a special class for labels and errors for a zend-form-element for example html output code before add classes

<dt id="username-label"><label for="username" class="required">user name:</label></dt>
<dd id="username-element">
<input type="text" name="username" id="username" value="" class="input" />
<ul class="errors"><li>Value is required and can't be empty</li></ul></dd>

and code after we add classes

<dt id="username-label"><label for="username" **class="req-username"**>user name:</label></dt>
<dd id="username-element">
<input type="text" name="username" id="username" value="" class="input" />
<ul **class="err-username"**><li>Value is required and can't be empty</li></ul></dd>

thanks


回答1:


What you need to do is modify the Label and Errors decorators for the Username element:

My\App\Form.php:

public function init() {
    // Init form and elements here
    // ...

    $username = new Zend_Form_Element_Text('username');
    $username
        ->setLabel('Username:')
        ->addDecorator('Label', array('class' => 'req-username'))
        ->addDecorator('Errors', array('class' => 'err-username'));

    // ...
}



回答2:


The Label decorator just calls to the view helper formLabel() behind the scenes. You can create your own view helper to override formLabel() to add the class.



来源:https://stackoverflow.com/questions/2595000/how-to-add-special-class-for-labels-and-errors-on-zend-form-elements

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!