dongnaoxia0927
2017-05-18 10:21
浏览 26
已采纳

字段集内的zf2 TwbBundle按钮组

I am working with zf2 and the TwbBundle. I wanto to add two buttons as a button group to the end of various forms. When I directly add them to the Form as two objects with the button-group option set, then it is rendered fine.

$this->add(array(
    'name' => 'submit',
    'type' => 'Button',
    'options' => array(
        'label' => 'Speichern',
        'button-group' => 'group-1',
    ),
    'attributes' => array(
        'type' => 'submit',
        'class' => 'btn btn-primary btn-lg',
    ),
));

$this->add(array(
     'name' => 'cancel',
     'type' => 'Button',
     'options' => array(
         'label' => 'Abbrechen',
         'button-group' => 'group-1',
     ),
    'attributes' => array(
         'type' => 'submit',
         'class' => 'btn btn-default btn-lg',
    ),
));

This results in:

<div class="form-group ">
    <div class="btn-group">
        <button type="submit" name="submit" class="btn btn-primary btn-lg" value="">Speichern</button>
        <button type="submit" name="cancel" class="btn btn-default btn-lg" value="">Abbrechen</button>
    </div>
</div>

But once I extract them into a reusable fieldset each of the elements gets wrapped in its own form-group element and no longer renders as a button group.

<fieldset>
    <div class="form-group ">
        <button type="submit" name="form-controls[submit]" class="btn btn-primary btn-lg" value="">Speichern</button>
    </div>
    <div class="form-group ">
        <button type="submit" name="form-controls[cancel]" class="btn btn-default btn-lg" value="">Abbrechen</button>
    </div>
</fieldset>

I tried adding css classes or button-group options to the fieldset in the form class, but nothing had the desired effect.

Anyone had the same problem or might have an idea on how to realize this?

Cheers Jens

Edit: On request additional code. How the fieldset is defined:

<?php
namespace Application\Form;

use Zend\Form\Fieldset;

class FormControls extends Fieldset
{
    public function __construct()
    {
        parent::__construct('form-controls');

        $this->add(array(
            'name' => 'submit',
            'type' => 'Button',
            'options' => array(
                'label' => 'Speichern',
                'button-group' => 'group-1',
            ),
            'attributes' => array(
                'type' => 'submit',
                'class' => 'btn btn-primary btn-lg',
            ),
        ));

        $this->add(array(
            'name' => 'cancel',
            'type' => 'Button',
            'options' => array(
                'label' => 'Abbrechen',
                'button-group' => 'group-1',
            ),
            'attributes' => array(
                'type' => 'submit',
                'class' => 'btn btn-default btn-lg',
            ),
        ));
    }
}

And included with this:

$this->add(array(
    'name' => 'form-controls',
    'type' => 'Application\Form\FormControls',
));
  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • douke7274 2017-05-19 19:53
    已采纳

    The reason you are getting the extra fieldset tag is, because you are adding your elements via a fieldset.

    class FormControls extends Fieldset

    $this->add(array(
        'name' => 'form-controls',
        'type' => 'Application\Form\FormControls',
    ));
    

    The solution would be to have the (form/fieldset) class which adds:

    $this->add(array(
        'name' => 'form-controls',
        'type' => 'Application\Form\FormControls',
    ));
    

    To extend a (form/fieldset) class containing:

    $this->add(array(
        'name' => 'submit',
        'type' => 'Button',
        'options' => array(
            'label' => 'Speichern',
            'button-group' => 'group-1',
        ),
        'attributes' => array(
            'type' => 'submit',
            'class' => 'btn btn-primary btn-lg',
        ),
    ));
    
    $this->add(array(
         'name' => 'cancel',
         'type' => 'Button',
         'options' => array(
             'label' => 'Abbrechen',
             'button-group' => 'group-1',
        ),
        'attributes' => array(
             'type' => 'submit',
             'class' => 'btn btn-default btn-lg',
        ),
    ));
    

    So you can have your reusable buttons. The result would be a structure like: CoolForm extends ButtonForm extends Form, in which:

    • Form would be the Zend class
    • ButtonForm is the class defining and adding the buttons
    • CoolForm is the form actually userd, which inherits the buttons without the fieldset wrapping them
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题