【问题标题】:Form Element Decorators表单元素装饰器
【发布时间】:2012-08-09 08:31:03
【问题描述】:

我一直在尝试使用 Zend_Form 和 Decorators 创建一个自定义表单,所需的 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 中

【问题讨论】:

    标签: php zend-framework zend-form zend-decorators


    【解决方案1】:

    而不是

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

    使用

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

    【讨论】:

      【解决方案2】:

      试试这个

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

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

      或者这个

      how to add special class for labels and errors on zend form elements?

      【讨论】:

      • 我这里的问题被误解了,我是用Zend Decorators来替换默认布局OL-LI布局,用div的。您建议的解决方案是将所述类添加到元素本身而不是包装标签标签的 div 中。
      猜你喜欢
      • 2012-04-15
      • 1970-01-01
      • 2013-07-25
      • 1970-01-01
      • 1970-01-01
      • 2011-01-10
      • 1970-01-01
      • 2011-09-09
      • 2012-01-29
      相关资源
      最近更新 更多