How to set first element of checkbox list checked?

Hi guys, a trivial question, but I don’t get it answered.

My intention is to set first element of following CheckBoxList being checked as standard value, all other should be unchecked.

Any ideas, how to get realized my intention?





    <div class="col-md-12">

        <?=

        $form->field($ZMailAusgangEmpfaenger, 'id_empfaenger_art[]')->checkboxList(

                [1 => 'an:', 2 => 'bc:', 3 => 'bcc:']);

        ?>

    </div>



Something like this will do:




$model = ....

$model->id_empfaenger_art = [

        1  // when you want 'an:' to be checked

    ];



I don’t understand, what U explain me with this statement. I want to be checked checkBox. As it seems ,U assign value to propertie. How sholud this operate?

Where sholud I code like this - in Controller or in formular?


$ZMailAusgangEmpfaenger->id_empfaenger_art = [1];



[size=“2”]Remember: I don’t want to set default value. I just wanne activate checkbox in formular[/size]

In the above, $ZMailAusgangEmpfaenger is the model for the form and ‘id_empfaenger_art’ is the attribute for the field which should receive the ids that the user has selected with the checkbox list.

We can set a default set of selected ids to the field before we render the form, probably somewhere in the controller (it doesn’t matter much).

That’s it. It should render the ‘an:’ checkbox checked.

I don’t understand what you mean by this. What’s the difference between ‘checked by default’ and ‘activated’?

This means, that default value will be written into database without signing up in formuar. Activating checkBox means, that user will see check-box fiield, which is activated. Saving value into database is same in both cases…

[color="#1C2837"][size="2"] I[/size][/color]

As I said, set the default values (or, initial values) to the attribute of the model before you render the checkbox list, then you will see the corresponding checkboxes initially checked (or, activated, in your term) in the form.




// check 'an:' and 'bc:' by default

$ZMailAusgangEmpfaenger->id_empfaenger_art = [1, 2];



BTW, could you show us what ‘id_empfaenger_art’ attribute is like? As you see in the example code, ‘id_empfaenger_art’ must hold an array of ids.

I will get following output, setting


var_dump($ZMailAusgangEmpfaenger->id_empfaenger_art);

after load()-method of framework:


array(3) {  [0]=>  string(1) "1"  [1]=>  string(1) "1"  [2]=>  string(1) "1"}

As U can see, id_empfaenger_art doesn’t hold an array of ids. It holds an array, whether checkBox has been checked or not checked. In this case, all checkBoxes have been checked. I assume, that’s what U wanted to say with ‘array of ids’, isn’t it?

For me, [size=“2”]‘array of ids’ means array of PrimaryKey…[/size]

[size=“2”]In any case, ur suggestion doesn’t work. I code like this both,in Controller and in formular:[/size]

[size="2"]


$ZMailAusgangEmpfaenger->id_empfaenger_art = [1, 2];

[/size]

[size="2"]This is output of Firedebug for last checkbox:[/size]

[size="2"]


<input name="ZMailAusgangEmpfaenger[id_empfaenger_art][]" value="3" data-index="2" type="checkbox">

[/size]

[size=“2”]Both checkBoxes won’t be checked loading formular[/size]

P.S.: I’m rendering several models in Controller. $ZmailAusgangEmpfaenger is one of them

Please check this thread.

http://www.yiiframework.com/forum/index.php/topic/53418-checkboxlist-options/

Try this:




    <div class="col-md-12">

        <?php

        $list = [0 => 'an', 1 => 'bc', 2 => 'bcc'];

        $list2 = [0, 1];

        $ZMailAusgangEmpfaenger->id_empfaenger_art = $list2;

        ?>

        <?=

        $form->field($ZMailAusgangEmpfaenger, 'id_empfaenger_art')->checkboxList($list);

        ?>

    </div>



Ahhh, that’s it. I have exact same problem and also for me problem got solved in same way shown up in this thread. I coded like this in formular:


    <div class="col-md-12">

        <?=

        $form->field($ZMailAusgangEmpfaenger, 'id_empfaenger_art')->checkboxList(array(0 => 'an', 1 => 'bc', 2 => 'bcc'));

        ?>

    </div>

in Controller:





        $ZMailAusgangEmpfaenger = new ZMailAusgangEmpfaenger();

        $ZMailAusgangEmpfaenger->id_empfaenger_art = [0];



So, this thread can be closed as succesfully solved.

softdark, once more again greetings to Japan combined with very sincere