问题
I'm currently styling form elements with a custom CSS class to style text inputs differently, as in:
$submit = new Zend_Form_Element_Submit('login');
$submit->setLabel('Log in')
->setAttrib('class', 'submit');
And
$username = new Zend_Form_Element_Text('username');
$username->setLabel('Username')
->setAttrib('class', 'textinput');
But let's say I have multiple forms, and want to style all text elements with textinput
and all submit elements with submit
by default. Is there anyway to do this globally?
回答1:
I'm no Zend expert, but I guess you could subclass each Zend_Form_Element_* class and setting the attributes you want to set in their constructors - i.e subclass Zend_Form_Element_Text in Zend_Form_Element_Text_Yatta; then setting attribute 'class' to 'textinput' in its constructor.
回答2:
Instead of setting classes on each different type of object use CSS to style the elements:
input[type="submit"] {
/* Here goes the stuff that you put in your submit class */
}
input[type="text"] {
/* here goes the stuff you put in your textinput class */
}
textarea {
/* here goes the stuff for a text area */
}
This will do what you want it to do and you don't have to sub-class the Zend Framework default helpers. Also, it will output less HTML so your pages will be smaller and the CSS can be re-used for each and every input element on the page.
来源:https://stackoverflow.com/questions/646272/style-form-elements-in-zend-framework-with-a-default-style