简体   繁体   English

Symfony 2 / Twig和复选框的集合

[英]Symfony 2 / Twig and a collection of checkboxes

In my application I need to display a group of checkboxes with which the user can indicate certain selections. 在我的应用程序中,我需要显示一组复选框,用户可以使用它们指示某些选择。 In my case it refers to "zones", ie Zone A, Zone B, Zone C, etc. 在我的情况下,它指的是“区域”,即区域A,区域B,区域C等。

The problem I'm running into is how to construct this in my form type class using the FormBuilderInterface provided by the framework. 我遇到的问题是如何使用框架提供的FormBuilderInterface在我的表单类型类中构造它。

The documentation refers to a "collection" type [1] which seems what I need but I'm having trouble to connect the checkbox element [2]. 该文档引用的是“集合”类型[1],这似乎是我所需要的,但我无法连接复选框元素[2]。 The documentation only seems to give an example for a single checkbox but I need it for a group. 该文档似乎仅提供了一个复选框的示例,但我需要一个组的示例。

This is what I have so far (I've left out the other fields for brevity): 到目前为止,这是我所拥有的(为了简洁起见,我省略了其他字段):

class FormType extends AbstractType {

  public function buildForm( FormBuilderInterface $builder, array $options )
  {
    $builder
      ->add('zones', 'collection',
        array(
          'type' => 'checkbox',
          'options'  => array(
            'zone-a' => 'Zone A',
            'zone-b' => 'Zone B',
            'zone-c' => 'Zone C',
          )
        )
      )
    ;
  }

}

And the data class (form model if you will): 以及数据类(如果需要的话,为表单模型):

class FormData {

  protected $zones = [];

  public function __construct( array $zones = NULL )
  {
    if( ! empty( $zones ) )
    {
      $this->setZones( $zones );
    }
  }

  public function getZones()
  {
    return $this->zones;
  }

  public function setZones( $zones )
  {
    $this->zones = $zones;
  }

}

This is how I render the form element (for now): 这就是我渲染表单元素的方式(目前):

{{ form_row(form.zones) }}

However, the above only outputs a label named Zones and nothing else. 但是,以上仅输出名为Zones的标签,而没有输出其他标签。

How do I correctly render a group/collection of checkboxes in a Symfony 2 / Twig application? 如何在Symfony 2 / Twig应用程序中正确呈现复选框的组/集合?

[1] http://symfony.com/doc/current/reference/forms/types/collection.html [2] http://symfony.com/doc/current/reference/forms/types/checkbox.html [1] http://symfony.com/doc/current/reference/forms/types/collection.html [2] http://symfony.com/doc/current/reference/forms/types/checkbox.html

You will need a second FormType. 您将需要第二个FormType。

The first form which you are calling should look something like this: 您要调用的第一种形式应如下所示:

$builder
  ->add('zones', 'collection',
    array(
      'type' => new FormDataType(),
      'options'  => array(
         .....
      )
    )
  )

And then the second form should look like this: 然后第二种形式应如下所示:

class FormDataType extends AbstractType  {
 ...
   $builder
      ->add('checkBox1', 'checkbox', ...)
      ->add('checkBox2', 'checkbox', ...)
      ....
}

I just wrote this, so I can not tell if it will work,... but this is the approach you will have to go, I have already done something like that in one of my projects 我只是写了这个,所以我不知道它是否可以工作,但是这是您必须采用的方法,我已经在我的一个项目中做了类似的事情

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM