Ken Le
Ken Le

Reputation: 1805

Zend Form Elements Add Attribute for all Elements

/* Form Elements & Other Definitions Here ... */
$this->setAction("auth")
    ->setMethod("post")
    ->setAttrib("class","ajax_form");


$email = new Zend_Form_Element_Text("email");
$email->setAttrib("class","text_input")
        ->setLabel("E-Mail")
        ->addValidator("EmailAddress","NotEmpty")
        ->isRequired(true);

$password = new Zend_Form_Element_Password("password");
$password->setAttrib("class","text_input")
        ->setLabel("Password")
        ->addValidator("NotEmpty")
        ->isRequired(true);

$this->addElements(array($email,$password));
$this->setDecorators(array(
    'FormElements',
    'FormErrors',
    array('HtmlTag',array('tag'=>'<table>')),
    'Form'
));

$this->setElementDecorators(array(
    'ViewHelper',
    array(array('data'=>'HtmlTag'), array('tag'=>'td')),
    array('Label',array('tag'=>'td')),
    array(array('row'=>'HtmlTag'), array('tag'=>'tr'))
));

I want add class "text_input" to all elements of this form on 1 line coding. I don't like to use setAtttrib for each element. Anyway ? I'm new on Zend.

Upvotes: 2

Views: 6365

Answers (1)

Rob Allen
Rob Allen

Reputation: 12778

There isn't a one-liner for this. If you have lots of elements, then the easiest way is to iterate over the elements after creation:

foreach ($this->getElements() as $element) {
    if ($element instanceof Zend_Form_Element_Text
        || $element instanceof Zend_Form_Element_Textarea
        || $element instanceof Zend_Form_Element_Password) {
        $element->setAttrib("class","text_input");
    }
}

Upvotes: 4

Related Questions