Arni Gudjonsson
Arni Gudjonsson

Reputation: 544

Zend Framework 2 formInput or formElement ID tag not rendering

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

Upvotes: 0

Views: 9621

Answers (2)

Guillermo Luque
Guillermo Luque

Reputation: 1426

Actually this code:

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

could be used correctly by a css renderer like Bootstrap due to fact that the $this->formRow(...) form helper will produce:

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

which is more readable than the original formRow output (without id neither class attributes)

Upvotes: 3

Rob Allen
Rob Allen

Reputation: 12778

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

Upvotes: 4

Related Questions