Zend Form Rendering and Decorators (Use correctly with bootstrap)

余生颓废 提交于 2020-02-02 14:00:50

问题


The Bootstrap Example Code

http://getbootstrap.com/css/#forms

Copying a simple email input element from getbootstrap.com suggests we format the HTML in the following way:

<div class="form-group">
    <label for="exampleInputEmail1">Email address</label>
    <input id="exampleInputEmail1" class="form-control" type="email" placeholder="Email">
</div>

Above we have a <label> tag that closes straight after it's text content, "Email address".

I would now like to create the same form group using Zend Framework.


module/MyApp/src/MyModule/Form/MyForm.php

namespace MyModule\Form;

use Zend\Form\Form;

class MyModuleForm extends Form {

 public function __construct($name = null)
 {

     $this->add(array(
         'name' => 'email_address',
         'type' => 'Email',             
         'options' => array(
             'label' => 'Email address',
         ),
         'attributes' => array(
            'class'  => 'form-control',
            'placeholder' => 'Email'

         )                
     ));

The Zend Framework generated code

<div class="form-group">
    <label>
        <span>Email address</span>
        <input class="form-control" type="email" placeholder="Email" id="exampleInputEmail1">
    </label>
</div>

In the above HTML you can see that Zend has not closed the <label> tag. Instead the <label> ecapsulates its children.

How do I change the way the Zend rendering works?


回答1:


I assume you are using ZF2 FormRow view helper to render your form element in your view script, e.g. $this->formRow($this->form->get('email_address'));

To render it differently you need to use the following view helpers

FormLabel

FormText

FormElementErrors

If for example you wanted to render as a definition list you would use something like

<dl class="zend_form">
    <dt><?php echo $this->formLabel($this->form->get('email_address')); ?></dt>
    <dd><?php echo $this->formText($this->form->get('email_address')); ?>
        <?php echo $this->formElementErrors($this->form->get('email_address')); ?></dd>
</dl>

I hope this points you in the right direction



来源:https://stackoverflow.com/questions/35641597/zend-form-rendering-and-decorators-use-correctly-with-bootstrap

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!