问题
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