字段集

时间:2017-05-18 10:21:54

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

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

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

但是一旦我将它们提取到可重用的字段集中,每个元素都会被包装在自己的form-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>

我尝试将css类或按钮组选项添加到表单类中的fieldset,但没有任何效果。

任何人都有同样的问题,或者对如何实现这个问题有所了解?

干杯 延

编辑:根据要求提供其他代码。如何定义字段集:

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

1 个答案:

答案 0 :(得分:1)

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

  

class FormControls扩展了Fieldset

require 'prometheus/client'

class RandomnumbersController < ApplicationController
  def index
    @randomnumbers = Randomnumber.order('number DESC').limit(8)
    @counter = 0
  end

  def show
    @randomnumber = Randomnumber.find(params[:id])
  end

  def new
  end

  def create
    @randomnumber = Randomnumber.new(randomnumber_params)

    prometheus = Prometheus::Client.registry
    begin
      @requests = prometheus.gauge(:demo, 'Random number selected for this users turn.')
    rescue Prometheus::Client::Registry::AlreadyRegisteredError => e
    end
    @requests.set({name: "test"}, 123)
    test =  @requests.get name: "test"

    @randomnumber.save
    redirect_to @randomnumber
  end

  private
  def randomnumber_params
    params.require(:randomnumber).permit(:name, :number)
  end
end

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

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

扩展包含以下内容的(form / fieldset)类:

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

所以你可以拥有可重复使用的按钮。结果将是如下结构: CoolForm 扩展 ButtonForm 扩展表单,其中:

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