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

报告相同问题?

悬赏问题

  • ¥30 求给定范围的全体素数p的(p-2)的连乘积
  • ¥15 VFP如何使用阿里TTS实现文字转语音?
  • ¥100 需要跳转番茄畅听app的adb命令
  • ¥50 寻找一位有逆向游戏盾sdk 应用程序经验的技术
  • ¥15 请问有用MZmine处理 “Waters SYNAPT G2-Si QTOF质谱仪在MSE模式下采集的非靶向数据” 的分析教程吗
  • ¥50 opencv4nodejs 如何安装
  • ¥15 adb push异常 adb: error: 1409-byte write failed: Invalid argument
  • ¥15 nginx反向代理获取ip,java获取真实ip
  • ¥15 eda:门禁系统设计
  • ¥50 如何使用js去调用vscode-js-debugger的方法去调试网页