0

我一直在尝试使用 Zend_Form 和装饰器创建自定义表单,所需的 html 格式如下:

<form enctype="application/x-www-form-urlencoded" method="post" action="">
    <div class="login_bx">
        <div id="txtAccount-label" class="txt_field">
            <label for="txtAccount" class="required">Account ID:</label>
        </div>
        <div class="inp_field">
            <input type="text" name="txtAccount" id="txtAccount" value="" style="width:250px">
        </div>
    </div>
</form>

Zend_Form 类的内容如下:

$account    =   new Zend_Form_Element_Text('txtAccount');
        $account    ->  setLabel('Account ID:')
                    ->  setAttrib('style','width:250px')
                    ->  setRequired(true)
                    ->  addValidator('NotEmpty', true);

        $this->setDecorators(array(
            'FormElements',
            array(
                'HtmlTag', 
                array(
                    'tag'   =>  'div',
                    'class' =>  'login_bx',
                )
            ),
            'Form',
            array('FormErrors', array(
                'placement'                 =>  'prepend', 
                'markupElementLabelEnd'     =>  '</strong>', 
                'markupElementLabelStart'   =>  '<strong>', 
                'markupListStart'           =>  '<div class="errors_list" id="msg">', 
                'markupListEnd'             =>  '</div>', 
                'markupListItemStart'       =>  '<div class="error_item">', 
                'markupListItemEnd'         =>  '</div>'
            ))
        ));

        $this->addElements(array($account));

        $this->setElementDecorators(array(
            'ViewHelper',
            //'Errors',
            array(array(
                'data' => 'HtmlTag'
                ), array(
                    'tag'   =>  'div',
                    'class' =>  'inp_field'
                )
            ),
            array('Label', array(
                'tag'   =>  'div',
                'class' =>  'txt_field'
            ))
        ));

当前呈现的html如下:

<form enctype="application/x-www-form-urlencoded" method="post" action="">
    <div class="login_bx">
        <div id="txtAccount-label">
            <label for="txtAccount" class="txt_field required">Account ID:</label>
        </div>
        <div class="inp_field">
            <input type="text" name="txtAccount" id="txtAccount" value="" style="width:250px">
        </div>
    </div>
</form>

我不知道如何将类添加到包装标签的 DIV

4

2 回答 2

1

代替

array('Label', array(
                'tag'   =>  'div',
                'class' =>  'txt_field'
            ))

采用

array('Label', array(
                'tag'   =>  'div',
                'tagClass' =>  'txt_field'
            ))
于 2012-08-12T12:09:48.167 回答
0

试试这个

$name = new Zend_From_Element_Text("txtName");
$name->setOptions(array('class'=>'css class name'));

http://forums.zend.com/viewtopic.php?f=69&t=1367

或这个

如何为 zend 表单元素的标签和错误添加特殊类?

于 2012-08-09T12:37:52.077 回答