Zend Framework 2 formInput or formElement ID tag not rendering

Arni Gudjonsson picture Arni Gudjonsson · Sep 28, 2012 · Viewed 9.6k times · Source

In Zend framework 2, when I use the view's formRow method like so

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

it will generate HTML like this

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

but if I use 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'));

i don't get the id tag

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

I've tried with formElement with same results.

How can I get it to render just the input with all attributes and values?

This is how my Zend Framework 2 View looks like (simplified)

<?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(); ?>

and the Zend Framework 2 Form

<?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'
            ),
        ));
    }
}
?>

Answer

Rob Allen picture Rob Allen · Sep 29, 2012

Change:

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

to:

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