Search code examples
checkboxsymfonyformbuilder

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


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?


Solution

  • You would simply set the value in your model or entity to true and than pass it to the FormBuilder then it should be checked.

    If you have a look at the first example in the documentation:

    A new task is created, then setTask is executed and this task is added to the FormBuilder. If you do the same thing with your checkbox

    $object->setCheckboxValue(true);
    

    and pass the object you should see the checkbox checked.

    If it's not working as expected, please get back with some sample code reproducing the error.