ZF2 how to wrap content in form fieldset?

荒凉一梦 提交于 2020-01-01 11:39:10

问题


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?


回答1:


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...



来源:https://stackoverflow.com/questions/15822845/zf2-how-to-wrap-content-in-form-fieldset

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