Yii Framework Forum: How to set first element of checkbox list checked? - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

How to set first element of checkbox list checked? [solved]

#1 User is offline   tklustig 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 419
  • Joined: 20-April 17
  • Location:Hannover

Posted 08 March 2018 - 08:18 AM

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>

0

#2 User is offline   softark 

  • Keep It Simple
  • Yii
  • Group: Moderators
  • Posts: 3,639
  • Joined: 16-February 11
  • Location:Japan

Posted 08 March 2018 - 05:46 PM

Something like this will do:

$model = ....
$model->id_empfaenger_art = [
        1  // when you want 'an:' to be checked
    ];

0

#3 User is offline   tklustig 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 419
  • Joined: 20-April 17
  • Location:Hannover

Posted 08 March 2018 - 08:14 PM

View Postsoftark, on 08 March 2018 - 05:46 PM, said:

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];

Remember: I don't want to set default value. I just wanne activate checkbox in formular
0

#4 User is offline   softark 

  • Keep It Simple
  • Yii
  • Group: Moderators
  • Posts: 3,639
  • Joined: 16-February 11
  • Location:Japan

Posted 08 March 2018 - 09:52 PM

Quote

?=
    $form->field($ZMailAusgangEmpfaenger, 'id_empfaenger_art[]')->checkboxList(
        [1 => 'an:', 2 => 'bc:', 3 => 'bcc:']);
?>



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).

View Posttklustig, on 08 March 2018 - 08:14 PM, said:

$ZMailAusgangEmpfaenger->id_empfaenger_art = [1];


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

Quote

Remember: I don't want to set default value. I just wanne activate checkbox in formular

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

#5 User is offline   tklustig 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 419
  • Joined: 20-April 17
  • Location:Hannover

Posted 09 March 2018 - 12:45 PM

View Posttklustig, on 08 March 2018 - 09:52 PM, said:

don't want to set default value. I just wanne activate checkbox in formular


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...

I
0

#6 User is offline   softark 

  • Keep It Simple
  • Yii
  • Group: Moderators
  • Posts: 3,639
  • Joined: 16-February 11
  • Location:Japan

Posted 09 March 2018 - 06:40 PM

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.
0

#7 User is offline   tklustig 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 419
  • Joined: 20-April 17
  • Location:Hannover

Posted 11 March 2018 - 03:34 AM

View Postsoftark, on 09 March 2018 - 06:40 PM, said:

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, 'array of ids' means array of PrimaryKey...
In any case, ur suggestion doesn't work. I code like this both,in Controller and in formular:
$ZMailAusgangEmpfaenger->id_empfaenger_art = [1, 2];


This is output of Firedebug for last checkbox:
<input name="ZMailAusgangEmpfaenger[id_empfaenger_art][]" value="3" data-index="2" type="checkbox">

Both checkBoxes won't be checked loading formularPosted Image
P.S.: I'm rendering several models in Controller. $ZmailAusgangEmpfaenger is one of them
0

#8 User is offline   softark 

  • Keep It Simple
  • Yii
  • Group: Moderators
  • Posts: 3,639
  • Joined: 16-February 11
  • Location:Japan

Posted 11 March 2018 - 05:37 AM

Please check this thread.
http://www.yiiframew...oxlist-options/
1

#9 User is offline   softark 

  • Keep It Simple
  • Yii
  • Group: Moderators
  • Posts: 3,639
  • Joined: 16-February 11
  • Location:Japan

Posted 11 March 2018 - 08:38 AM

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>

1

#10 User is offline   tklustig 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 419
  • Joined: 20-April 17
  • Location:Hannover

Posted 11 March 2018 - 08:45 AM

View Postsoftark, on 11 March 2018 - 05:37 AM, said:

Please check this thread.
http://www.yiiframew...oxlist-options/

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




1

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users