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 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
  • ¥15 相敏解调 matlab
  • ¥15 求lingo代码和思路
  • ¥15 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应