0

在 Zend 框架 2 中,当我像这样使用视图的formRow方法时

$this->formRow($form->get('product_name'));

它会生成这样的 HTML

<label for="product_name">
    <span>Name</span>
    <input type="text" id="product_name" name="product_name">
</label>

但是如果我使用formInput

<div class="control-group">
    <?php echo $this->formLabel($form->get('product_name')->setLabelAttributes(array('class'=>'control-label'))); ?>
    <div class="controls">
        <?php echo $this->formInput($form->get('product_name')); ?>
    </div>
</div>

$this->formInput($form->get('product_name'));

我没有拿到身份证

<input type="" name="product_name">

我尝试使用 formElement 获得相同的结果。

我怎样才能让它只呈现具有所有属性和值的输入?

这就是我的Zend Framework 2 视图的样子(简化)

<?php echo $this->form()->openTag($form); ?>    
<div class="control-group">
    <?php echo $this->formLabel($form->get('product_name')->setLabelAttributes(array('class'=>'control-label'))); ?>
    <div class="controls"><?php echo $this->formInput($form->get('product_name')); ?></div>
</div>
<div class="control-group">
    <div class="controls"><?php echo $this->formSubmit($form->get('submit')); ?></div>
</div>

<?php echo $this->form()->closeTag(); ?>

Zend Framework 2 表单

<?php
namespace Product\Form;

use Zend\Form\Form;

class ProductForm extends Form
{
    public function __construct($name = null)
    {
        // we want to ignore the name passed
        parent::__construct('product');
        $this->setAttribute('method', 'post');
        $this->setAttribute('class','form-horizontal');

        $this->add(array(
            'name' => 'product_name',
            'attributes' => array(
                'type'  => 'text',
            ),
            'options' => array(
                'label' => 'Name',
            ),
        ));

        $this->add(array(
            'name' => 'submit',
            'attributes' => array(
                'type'  => 'submit',
                'value' => 'Save',
                'id' => 'submitbutton',
                'class'=>'btn btn-success btn-large'
            ),
        ));
    }
}
?>
4

2 回答 2

4

改变:

    $this->add(array(
        'name' => 'product_name',
        'attributes' => array(
            'type'  => 'text',
        ),
        'options' => array(
            'label' => 'Name',
        ),
    ));

到:

    $this->add(array(
        'name' => 'product_name',
        'attributes' => array(
            'type'  => 'text',
            'id'    => 'product_name',
        ),
        'options' => array(
            'label' => 'Name',
        ),
    ));
于 2012-09-29T09:19:19.063 回答
3

其实这段代码:

 $this->add(array(
        'type' => 'Zend\Form\Element\Text',
        'name' => 'product_name',
        'attributes' => array(
            'id'    => 'product_name',
            'class' => 'span3',
        ),
        'options' => array(
            'label' => 'Your label',            
        ),
    ));

可以被像 Bootstrap 这样的 css 渲染器正确使用,因为$this->formRow(...)表单助手会产生:

<label for="product_name">Your label</label><input type="text" name="product_name" class="span3" id="product_name" value="">

这比原始formRow输出更具可读性(没有 id 和类属性)

于 2012-11-21T04:16:06.410 回答