2012-04-19 14 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行のコードを追加します。 各要素にsetAtttribを使用するのは嫌です。とにかく?私はZendの新作です。Zend Form Elementsすべての属性を属性に追加

答えて

4

これには1つのライナーはありません。たくさんの要素がある場合は、作成後に要素を繰り返し処理するのが一番簡単な方法です:

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、私は本当にループを使いたくありません。しかし、それは知ってうれしいです。 –

関連する問題