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行のコーディングで、このフォームのすべての要素にクラス「text_input」を追加したい。各要素に setAtttrib を使用するのは好きではありません。ともかく ?私はZendが初めてです。

4

1 に答える 1

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");
    }
}
于 2012-04-19T18:43:25.900 に答える