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

madc picture madc · Mar 6, 2012 · Viewed 64.2k times · Source

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?

Answer

Simon Kerr picture Simon Kerr · Jun 11, 2012

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

$builder->add('isPublic', 'checkbox', array(
    'attr' => array('checked'   => 'checked'),
));