views:

1997

answers:

2

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?

A: 

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.

Seb
Zend_Framework has a very specific naming scheme to make it easier for the user to sub-class and override default zend helpers and classes thereof.
X-Istence
+1  A: 

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.

X-Istence
I'm aware I can style elements with CSS, but if I remember correct and http://reference.sitepoint.com/css/attributeselector is correct as well, the attribute selector won't work in IE. Hence I can't use this solution.
Mads Mobæk
mobmad: Take a look at ie7-js by Dean Edwards to fix the issues IE has with selectors!
X-Istence