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
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog