duandu1377 2012-03-21 08:41
浏览 62

如何使用Zend Form Decorator将所有输入和标签放入div?

My code looks like:

$this->setElementDecorators(array(
   array('ViewHelper'),
   array('Errors', array('tag' => 'div', 'class' => 'error')),
   array('Label', array('tag' => 'span')),
   array('HtmlTag', array('tag' => 'div'))
));

And it gives me:

<div>
   <div id="email-label"><label for="email" class="required">First name:</label></div>
   <input type="text" name="email" id="email" value="" />
</div>
<div>
   <div id="email-label"><label for="email" class="required">Last name:</label></div>
   <input type="text" name="email" id="email" value="" />
</div>

But I would like to have everything in divs with specified classes, like this:

<div class="item">
   <div class="label"><label for="email" class="required">First name:</label></div>
   <div class="element"><input type="text" name="email" id="email" value="" /></div>
</div>
<div class="item">
   <div class="label"><label for="email" class="required">Last name:</label></div>
   <div class="element"><input type="text" name="email" id="email" value="" /></div>
</div>

How can I do this? I read the manual, but I don't have more ideas :(

  • 写回答

1条回答 默认 最新

  • duanla3319 2012-03-21 23:15
    关注

    Try:

    $this->setElementDecorators(array(
       array('ViewHelper'),
       array('Errors', array('tag' => 'div', 'class' => 'error')),
       array('Label', array('tag' => 'span')),
       array('HtmlTag', array('tag' => 'div', 'class' => 'label')),
    ));
    

    And then when you render your form, manually render it like this:

    <div class="form">
        <form method="<?php echo $this->form->getMethod() ?>"
              action="<?php echo $this->form->getAction() ?>">
    
            <div class="item">
                <?php echo $this->form->firstName ?></div>
            </div>
    
            <div class="item">
                <?php echo $this->form->lastName ?></div>
            </div>
    
            // the rest of your elements...
    
            <?php echo $this->form->submit ?>
        </form>
    </div>
    

    Outputting individual form elements in your view gives you more control over the output of the form and gives you much greater control of where the elements appear and the HTML that they are wrapped within.

    When I can, I will use the short <?php echo $this->form ?> but most of my forms are fancier and I use custom markup in conjunction with my element decorators to get the form to look exactly how I want.

    See also Using ViewScript Decorator on Nested Subforms (Zend Form) and How To Remove All DtDdWrappers and Labels on Zend Form Elements.

    评论

报告相同问题?

悬赏问题

  • ¥15 smptlib使用465端口发送邮件失败
  • ¥200 总是报错,能帮助用python实现程序实现高斯正反算吗?有偿
  • ¥15 对于squad数据集的基于bert模型的微调
  • ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
  • ¥20 steam下载游戏占用内存
  • ¥15 CST保存项目时失败
  • ¥15 树莓派5怎么用camera module 3啊
  • ¥20 java在应用程序里获取不到扬声器设备
  • ¥15 echarts动画效果的问题,请帮我添加一个动画。不要机器人回答。
  • ¥15 Attention is all you need 的代码运行