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

70,091

Solution 1

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.

Solution 2

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

$builder->add('isPublic', CheckboxType::class, array(
    'attr' => array('checked'   => 'checked'),
));

Solution 3

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

So:

$builder->add('checkboxName', 'checkbox', array('mapped' => false,
    'label' => 'customLabel',
    'data' => true, // Default checked
));

Solution 4

Setting the 'data' option works for me. I'm creating a non entity based form:

$builder->add('isRated','checkbox', array(
    'data' => true
));

Solution 5

In TWIG

If you wish to do this in the template directly:

{{ form_widget(form.fieldName, { 'attr': {'checked': 'checked'} }) }}
Share:
70,091

Related videos on Youtube

madc
Author by

madc

Updated on July 16, 2021

Comments

  • madc
    madc almost 3 years

    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?

  • madc
    madc over 12 years
    Well, i already do set a value of the form this way, but I was simply too focused on finding an attribute to set the checkbox that I did not even thought in this direction. Thanks for the fast and perfect answer, solved the problem!
  • linuxatico
    linuxatico almost 12 years
    works for me, I am looking now for the same thing but for a radiobutton
  • rjmunro
    rjmunro almost 12 years
    That's interesting. You are telling symfony to add checked="checked" to the attributes of the checkbox, rather than telling symfony that the value of the checkbox is true and that it should therefore be adding checked="checked" itself.
  • James
    James almost 12 years
    Works for me in Symfony2.0. Thanks!
  • insertusernamehere
    insertusernamehere over 11 years
    +1 - Another way, if true/checked is the default value for any new entity, is simply to set the value in the __construct()-method like: public function __construct() { $this->attribute = true; }.
  • Iazel
    Iazel almost 10 years
    Thanks for sharing; this should be the accepted answer :)
  • Iazel
    Iazel almost 10 years
    In this way the checkbox is impossible to uncheck
  • YumeYume
    YumeYume about 8 years
    Simple and efficient solution for Symfony 3. Many thanks !
  • Peter
    Peter about 8 years
    In Symfony 2.8, this doesn't work if the model has an explicit false value when creating the form. The checkbox would still be checked.
  • cg.
    cg. almost 8 years
    Yes, thanks Peter. Any idea on what will work in Symfony >=2.8?
  • userfuser
    userfuser almost 7 years
    But be aware that in the cases when you are showing the already existing object (with possibly this value NOT checked), your 'data' = true would still force it to be shown as checked. From the same doc page: "The data option always overrides the value taken from the domain data (object) when rendering. This means the object value is also overriden when the form edits an already persisted object, causing it to lose its persisted value when the form is submitted."
  • CunningFatalist
    CunningFatalist over 6 years
    Won't work if you want to use the form for editing an entity, too. A value of "false" will still have a "checked" attribute. Sgoettschkes' solution is better
  • nicolallias
    nicolallias about 6 years
    This solutions works for new AND edit ; from symfony2.3 to 4+ ; and is brilliantly simple! many thanks @stanislav ; The only modification I have (symfony 2.8) is to use CheckboxType::class instead of 'checkbox'
  • Zoltán Süle
    Zoltán Süle over 5 years
    symfony 4: this option determines what value the field will return when the submitted value is empty (or missing). It does not set an initial value if none is provided when the form is rendered in a view.