2012-04-19 162 views
2
/* 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')) 
)); 

我希望在1行編碼中爲此表單的所有元素添加class「text_input」。 我不喜歡爲每個元素使用setAtttrib。無論如何?我是Zend的新手。Zend表單元素爲所有元素添加屬性

回答

4

這沒有一行代碼。如果你有很多元素,那麼最簡單的方法就是在創建後迭代元素:

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"); 
    } 
} 
+0

ty,我真的不喜歡使用循環。但很高興知道。 – 2012-04-19 21:11:21