Zend表单装饰器添加类

时间:2014-05-15 07:40:09

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

我是Zend的新手,也是php的初学者。我有一个生成几个元素的表单。我已经添加了装饰器,将它们分别放在div中:

$element->addFilter('StringTrim')->addDecorators(array('clearfix'=>new Zend_Form_Decorator_HtmlTag(array('tag'=>'div','class'=>'clearfix'))));

我生成的html看起来像这样:

    <div class="clearfix">
        <dt id="email-label"><label for="email" class="optional">Email</label></dt>
        <dd id="email-element">
        <input type="text" name="email" id="email" value="" class="text" maxlength="100">    
        </dd> 
    </div>

我想在dd标签中添加一个类来实现:

    <div class="clearfix">
        <dt id="email-label"><label for="email" class="optional">Email</label></dt>
        <dd class="clearfix" id="email-element">
        <input type="text" name="email" id="email" value="" class="text" maxlength="100">    
        </dd> 
    </div>

编辑:

/* ###################################  email                ################################### */
    $email          = new Zend_Form_Element_Text('email');
    $email          ->setLabel("Email");
    $email          ->setAttrib('class','text');
 // $email          ->setRequired(true);
    $email          ->addValidator('EmailAddress');
    $email          ->setFilters(array('StringTrim', 'StringToLower'));
    $email          ->addFilter('StringTrim')->addDecorators(array('clearfix'=>new Zend_Form_Decorator_HtmlTag(array('tag'=>'div','class'=>'clearfix'))));
    $email          ->addValidator('StringLength', false, array(0, 100));
    $email          ->setAttrib('maxlength', '100');
    $this           ->addElement($email);

有人有想法吗? 谢谢!

3 个答案:

答案 0 :(得分:1)

试试这个:

$element = new Zend_Form_Element_Text('email'); 
$element->addDecorators(array(array('HtmlTag',array('tag' => 'dd', 'class' => 'yourclass' )));

答案 1 :(得分:0)

试试这个

$deco_html_tag = $element->getDecorator('HtmlTag');
$deco_html_tag->setOption('class', 'clearfix');

答案 2 :(得分:0)

这是不使用装饰器的另一种方法。

您可以在表单类中添加它。

$this->setElementDecorators(array(
       'ViewHelper'
));

在您的视图中渲染您的表单。

<form class="your-class" id="<?php echo $this->form->getAttrib('id'); ?>" name="<?php echo $this->form->getAttrib('name'); ?>" action="<?php echo $this->escape($this->form->getAction()); ?>" method="<?php echo $this->escape($this->form->getMethod()); ?>">

<div class="something"> 
     <?php echo $this->form->getElement('element')->getLabel();  ?>
</div>
<div class="something"> 
     <?php echo $this->form->getElement('element');  ?>
</div>
</form?