【问题标题】:zf2 TwbBundle button group inside fieldset字段集中的 zf2 TwbBundle 按钮组
【发布时间】:2017-05-20 03:53:42
【问题描述】:

我正在使用 zf2 和 TwbBundle。我想在各种表单的末尾添加两个按钮作为按钮组。当我直接将它们作为设置了按钮组选项的两个对象添加到表单中时,它就可以很好地呈现。

$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',
    ),
));

这会导致:

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

但是一旦我将它们提取到一个可重用的字段集中,每个元素都会被包装在自己的表单组元素中,并且不再呈现为按钮组。

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

我尝试在表单类的字段集中添加 css 类或按钮组选项,但没有任何效果。

有人遇到过同样的问题,或者可能知道如何实现这一点吗?

干杯 延斯

编辑:根据要求提供附加代码。字段集是如何定义的:

<?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',
            ),
        ));
    }
}

并包含在其中:

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

【问题讨论】:

  • 你能发布你如何使用你的可重用字段集吗?您是扩展一个类还是将字段集添加到另一个字段集?
  • 用附加代码编辑了问题。对于我在 Fieldset 类上扩展的字段集

标签: php twitter-bootstrap-3 zend-framework2


【解决方案1】:

您获得额外字段集标签的原因是,因为您是通过字段集添加元素。

类 FormControls 扩展字段集

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

解决方案是添加 (form/fieldset) 类:

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

扩展一个(表单/字段集)类,包含:

$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',
    ),
));

这样您就可以拥有可重复使用的按钮。结果将是这样的结构:CoolForm extends ButtonForm extends Form,其中:

  • Form 将是 Zend 类
  • ButtonForm 是定义和添加按钮的类
  • CoolForm 是实际用户使用的表单,它继承了按钮而没有包装它们的字段集

【讨论】:

  • 谢谢,这行得通。现在,我的两个按钮按应有的方式显示。虽然当我扩展我的 ButtonForm 类时,它们总是在我的表单的最顶端。我检查了这个问题,但优先级标志似乎对我不起作用(stackoverflow.com/questions/25811923/…)。知道如何将他们逼到最底层吗?
  • 在添加其他元素后,您应该调用添加按钮的父函数。
  • 非常感谢。像魅力一样工作。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-01-09
  • 1970-01-01
  • 2013-10-18
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多