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 回答