shivam pandya
shivam pandya

Reputation: 51

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?

Upvotes: 0

Views: 1401

Answers (2)

egeloen
egeloen

Reputation: 5874

You should use the expanded option and set it to 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.

Upvotes: 2

dexter
dexter

Reputation: 31

Select tag, Checkboxes or Radio Buttons field may be rendered as one of several different HTML fields, depending on the expanded and multiple options:

select tag => expanded = false ,multiple = false

select tag (with multiple attribute) => expanded = false, multiple = true

radio buttons => expanded = true, multiple = false

checkboxes => expanded = true, multiple = true

Refer this table for your requirement

Upvotes: 1

Related Questions