Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ZF2 how to wrap content in form fieldset?

I have form with fieldsets:

$formConfig = array(
    'fieldsets' => array(
        ...
    );
);

$factory = new Zend\Form\Factory();
$form = $factory->createForm($formConfig); 
echo $this->form($form);

It renders something like this:

<form>
    <fieldset>
        <legend>Fieldset label</legend>
        <label><span>Elem 1</span><input type="text" name="f1[el1]" /></label>
        <label><span>Elem 2</span><input type="text" name="f1[el2]" /></label>
        <label><span>Elem 3</span><input type="text" name="f1[el3]" /></label>
    </fielset>
</form>

The problem is that I need to wrap content after legend:

<form>
    <fieldset>
        <legend>Fieldset label</legend>
        <div class="wrapper">
            <label><span>Elem 1</span><input type="text" name="f1[el1]" /></label>
            <label><span>Elem 2</span><input type="text" name="f1[el2]" /></label>
            <label><span>Elem 3</span><input type="text" name="f1[el3]" /></label>
        <div>
    </fielset>
</form>

How can I do that?

like image 740
Ildar Avatar asked Apr 04 '13 22:04

Ildar


1 Answers

Once more you need to understand that a Zend\Form\Fieldset does not equal a HTML <fieldset>! A Zend\Form\Fieldset merely is a collection of Zend\Form\Element that usually represent one entity and you could provide several entities with data from one Form.

Now when it comes to rendering the form, the first thing you should learn about are the several Zend\Form\View\Helper-Classes. You are using the form() view-helper, which automatically translates all Zend\Form\Element using formRow() and all Zend\Form\Fieldset using formCollection(). But you don't want to do that!

When wanting your preferred output, you will be needed to render the form yourself. Something like this could be your view-template:

<?=$this->form()->openTag($form);?>
    <fieldset>
        <div class="wrapper">
            <?=$this->formRow($form->get('f1')->get('el1'));?>
            <?=$this->formRow($form->get('f1')->get('el2'));?>
            <?=$this->formRow($form->get('f1')->get('el3'));?>
        </div>
    </fieldset>
<?=$this->form()->closeTag();?>

Now, this already has a little comfort within it, as you'd be using formRow(). You could also split up each form-row and go the very detailled way like:

<label>
    <span><?=$this->formLabel($form->get('f1')->get('el1'));?></span>
    <?=$this->formInput($form->get('f1')->get('el1'));=>
    <?=$this->formElementErrors($form->get('f1')->get('el1'));?>
</label>

Even there, formInput() still is a magic that derives into things like formText(), formSelect(), formTextarea(), etc.., etc...

like image 99
Sam Avatar answered Sep 28 '22 17:09

Sam