简体   繁体   English

如何在Symfony2中显示数据库表中的复选框

[英]How to display a checkbox from a database table in Symfony2

I want to display a database value in a checkbox, during add and edit time 我想在添加和编辑期间在复选框中显示数据库值

Here is my code which displays values in combobox 这是我的代码,在组合框中显示值

public function buildForm(FormBuilderInterface $builder, array $options)
{

    $builder->add('role', 'entity', array(
    'class'         => 'DashboardAdminManageUserBundle:role',
    'property'      => 'title',
    'multiple'      => true,
    'query_builder' => function(EntityRepository $er) {
        return $er->createQueryBuilder('g');

    },
    'label'    => 'Role*:',
    'by_reference' => false,
    'required' => false,
    ));



}

So how do you display the same values in a Checkbox? 那么如何在复选框中显示相同的值?

You should use the expanded option and set it to true. 您应该使用扩展选项并将其设置为true。 Take a look at the documentation for more informations. 查看文档以获取更多信息。

$builder->add('role', 'entity', array(
    'class'    => 'DashboardAdminManageUserBundle:role',
    'property' => 'title',
    'expanded' => true,
    'multiple' => true,
    'label'    => 'Role*:',
    'required' => false,

    // Add custom html attribute
    'attr'     => array('class' => 'my-class'),
));

Then, just need to customize the .my-class input CSS. 然后,只需要自定义.my-class输入CSS。

Select tag, Checkboxes or Radio Buttons field may be rendered as one of several different HTML fields, depending on the expanded and multiple options: 选择标记,复选框或单选按钮字段可能呈现为几个不同的HTML字段之一,具体取决于展开的选项和多个选项:

select tag => expanded = false ,multiple = false 选择标签=>扩展=假,多个=假

select tag (with multiple attribute) => expanded = false, multiple = true 选择标签(具有多个属性)=>扩展=假,多个=真

radio buttons => expanded = true, multiple = false 单选按钮=>展开= true,多个= false

checkboxes => expanded = true, multiple = true 复选框=>扩展=真,多个=真

Refer this table for your requirement 请参考下表

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

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