How to render a checkbox that is checked by default with the symfony2 Form Builder?
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'} }) }}
Related videos on Youtube
madc
Updated on July 16, 2021Comments
-
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 over 12 yearsWell, 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 almost 12 yearsworks for me, I am looking now for the same thing but for a radiobutton
-
rjmunro almost 12 yearsThat'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 almost 12 yearsWorks for me in Symfony2.0. Thanks!
-
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 almost 10 yearsThanks for sharing; this should be the accepted answer :)
-
Iazel almost 10 yearsIn this way the checkbox is impossible to uncheck
-
YumeYume about 8 yearsSimple and efficient solution for Symfony 3. Many thanks !
-
Peter about 8 yearsIn 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. almost 8 yearsYes, thanks Peter. Any idea on what will work in Symfony >=2.8?
-
userfuser almost 7 yearsBut 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 over 6 yearsWon'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 about 6 yearsThis 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 over 5 yearssymfony 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.