Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to render a checkbox that is checked by default with the symfony2 Form Builder?

Tags:

I have not found any easy way to accomplish to simply check a Checkbox by default. That can not be that hard, so what am i missing?

like image 830
madc Avatar asked Mar 06 '12 12:03

madc


2 Answers

You can also just set the attr attribute in the form builder buildForm method:

$builder->add('isPublic', CheckboxType::class, array(     'attr' => array('checked'   => 'checked'), )); 
like image 100
Simon Kerr Avatar answered Oct 06 '22 01:10

Simon Kerr


In Symfony >= 2.3 "property_path" became "mapped".

So:

$builder->add('checkboxName', 'checkbox', array('mapped' => false,     'label' => 'customLabel',     'data' => true, // Default checked )); 
like image 41
Leto Avatar answered Oct 06 '22 01:10

Leto