how to set CheckBoxList defaults

Notice: This thread is very old.
mikeb
Member | 30
+
0
-

i can't find anywhere how to set defaults on a checkboxlist.

the forms page only specifies other inputs including select, radio, checkbox. setDefaultValue('val'); does not work with checkboxlist, and I can't see any methods in the CheckboxList.php class or MultiChoiceControl.php

any suggestions?
thanks
Mike

Pavel Kravčík
Member | 1047
+
+1
-

This works for sure:

$form->setDefaults(['checkboxListName' => [one => 0, two => 1]]);

Or https://api.nette.org/…Control.html#…, but you need use array I guess.

mikeb
Member | 30
+
0
-

thanks, but it doesn't work for me. I'm trying like this:

$form->setDefaults([$a_field => ["option-2"=>'1']]);

and get this error:

Uncaught exception 'Nette\InvalidArgumentException' with message 'Value '1' are out of allowed set ['option-1', 'option-2'] in field...

i think its generated by line 63: https://github.com/…eControl.php
but i can't yet follow the problem or see where ‘Value 1’ in that error is coming from
and setDeafultValue doens't work with my array either…

all ideas welcome
???

Last edited by mikeb (2016-11-01 10:25)

Pavel Kravčík
Member | 1047
+
0
-

And you tried without 1?

$form->setDefaults(['checkboxListName' => ['option-1', 'option-2']]);
mikeb
Member | 30
+
0
-

YES! that works

$form->setDefaults([$a_field => ["option-2"]]);

thanks+++

tonynguyen259
Member | 1
+
0
-

Thankss so much