Activeradiobuttonlist Not Populating Checked/unchecked On Form

I have a simple activeradiobuttonlist that is not showing any checked/unchecked values in the form on either initial page entry or a post back.


    <div class="row">

        <?php  echo CHtml::activeRadiobuttonlist($model,'comment_option', array('enable_commenting_by_everyone'=>'Allow everyone to comment.', 'enable_commenting_by_friends_only'=>'Allow only friends to comment.', 'disable_commenting'=>'Allow no one to comment.')); ?>

    </div>

enable_commenting_by_everyone,enable_commenting_by_friends_only and disable_commenting are database columns in the model.

Not sure what I am missing. Seems like it should just work.

Mike

What is comment_option then ?

The second parameter define the column, attribute of the model you are gonna fill with one of the value you give in array. (need a check though)

http://www.yiiframework.com/wiki/110/styling-radio-buttons/

to debug easily you can check the info in $_POST when you submit… you can do it easily with firebug if you’re using firefox or it’s integrated to chrome.

EDIT:

why not rather use, if you are in your form, $form->radioButtonList($model…); instead

Hi.

Comment_option holds the value of the selected radio button.


[Account] => Array

        (

            [show_toks_on_community_page] => 1

            [all_view_my_toks] => 1

            [send_email_on_tok_rating] => 1

            [allow_incoming_rating_email] => 1

            [ok_to_show_my_toks_on_landing_page] => 1

            [comment_option] => enable_commenting_by_everyone

        )

This is fine. I can easily add logic to update the different database columns but the model isn’t populating the radio buttons on initial page entry or on save. And, I can’t find any documentation that aids in setting up the parameters other than the way it currently is.

It is just me or is YII just poorly documented? I can’t find an example that fits my use case.

Why don’t I use $form->, what is the advantage of that? Maybe I should read up on it but frankly I’m very busy and simply need straightforward solutions instead of having to spend way more time futzing with very trivial stuff.

Thanks for the reply. I not totally sold on yii.

Mike


     <div class="row">

        <?php  echo CHtml::activeRadiobutton($model,'enable_commenting_by_everyone'); ?>

        <?php  echo CHtml::activeLabel($model,'enable_commenting_by_everyone'); ?>

    </div>

    <div class="row">

        <?php  echo CHtml::activeRadiobutton($model,'enable_commenting_by_friends_only'); ?>

        <?php  echo CHtml::activeLabel($model,'enable_commenting_by_friends_only'); ?>

    </div>

    <div class="row">

        <?php  echo CHtml::activeRadiobutton($model,'disable_commenting'); ?>

        <?php  echo CHtml::activeLabel($model,'disable_commenting'); ?>

    </div> 

POST array




[Account] => Array

        (

            [show_toks_on_community_page] => 1

            [all_view_my_toks] => 1

            [send_email_on_tok_rating] => 1

            [allow_incoming_rating_email] => 1

            [ok_to_show_my_toks_on_landing_page] => 1

            [enable_commenting_by_everyone] => 0

            [enable_commenting_by_friends_only] => 0

            [disable_commenting] => 1

        )



This passes the individual radio button values. The model does populate the radio buttons on initial page entry or on save as one would expect. But once I group the radio buttons, I lose that. I’m working on that example. Seems that the framework would be able to handle it. I’m not sure what I am missing here.

Unfortunately, I have to continue working on other examples until I find a reasonable solution. I’ll post anything I find that may be useful. Can someone point me to a relevant example?

Mike


   <div class="row">

        <?php  echo CHtml::activeRadiobutton($model,'enable_commenting_by_everyone', array('name'=>'commentrules','value'=>'enable_commenting_by_everyone')); ?>

        <?php  echo CHtml::activeLabel($model,'enable_commenting_by_everyone'); ?>

    </div>

    <div class="row">

        <?php  echo CHtml::activeRadiobutton($model,'enable_commenting_by_friends_only', array('name'=>'commentrules','value'=>'enable_commenting_by_friends_only')); ?>

        <?php  echo CHtml::activeLabel($model,'enable_commenting_by_friends_only'); ?>

    </div>

    <div class="row">

        <?php  echo CHtml::activeRadiobutton($model,'disable_commenting', array('name'=>'commentrules','value'=>'disable_commenting')); ?>

        <?php  echo CHtml::activeLabel($model,'disable_commenting'); ?>

    </div>

POST array




Array

(

    [Account] => Array

        (

            [show_toks_on_community_page] => 1

            [all_view_my_toks] => 1

            [send_email_on_tok_rating] => 1

            [allow_incoming_rating_email] => 1

            [ok_to_show_my_toks_on_landing_page] => 1

        )


    [commentrules] => Array

        (

            [0] => 0

            [1] => enable_commenting_by_everyone

            [2] => 0

            [3] => 0

        )


    [Save] => Save

)



Another attempt. Now the grouping of the radio buttons removes the radio button values from the Account model. So, the radio buttons on the page are never set.

Mike

I’m not sure to understand what you want exactly… you want to set 1 column among 3 different for the same model, right?

Thanks for your response.

I have an account settings page. On that page I have some checkboxes and then the three radio buttons.

Account settings page:4656

acct.settings.page.png

The checkboxes work fine. See below. They are populated with the correct values that are in the database. Each checkbox is mapped to a single database column. Each radio button is also mapped to a database column. The database columns for the radio are as follows:

  • enable_commenting_by_everyone

  • enable_commenting_by_friends_only

  • disable_commenting

I’ve not been unable to have success in doing the same for the radio buttons. See my previous posts. Since the radio buttons are a group, I only want one to be selected at a time and also want to have the one selected to be checked on initial page entry and any postback. That’s it. Very simple. Seems not so simple in YII.




<div class="form">

<?php echo CHtml::beginForm(); ?>

 

    <?php echo CHtml::errorSummary($model); ?>

 

    <div class="row">

        <?php echo CHtml::activeCheckBox($model,'show_toks_on_community_page') ?>

        <?php echo CHtml::activeLabel($model,'show_toks_on_community_page'); ?>


    </div>


    <div class="row">

        <?php echo CHtml::activeCheckBox($model,'all_view_my_toks'); ?>

        <?php echo CHtml::activeLabel($model,'all_view_my_toks'); ?>


    </div>


    <div class="row">

        <?php  echo CHtml::activeCheckBox($model,'send_email_on_tok_rating'); ?>

        <?php  echo CHtml::activeLabel($model,'send_email_on_tok_rating'); ?>


    </div>


    <div class="row">

        <?php  echo CHtml::activeCheckBox($model,'allow_incoming_rating_email'); ?>

        <?php  echo CHtml::activeLabel($model,'allow_incoming_rating_email'); ?>


    </div>


    <div class="row">

        <?php  echo CHtml::activeCheckBox($model,'ok_to_show_my_toks_on_landing_page'); ?>

        <?php  echo CHtml::activeLabel($model,'ok_to_show_my_toks_on_landing_page'); ?>


    </div>



@Yii Staff:

I have mixed emotions about YII. I’m at fault to a degree because I haven’t read every piece of documentation but then do I really need to do that. I am a fairly experienced web app developer. Give me an example and I can get it to work. I’m not finding relevant examples in YII documentation. Yeah, the class definitions are there but not relevant examples. This isn’t the first time I’ve run into this. I chose YII without a lot of testing but YII looks strong on the surface until you get into certain use cases they you have to spend an inordinate amount of time figuring out how to do some of the simplest things, like populating radio buttons from a database.

Mike

It is easy to get the selected value from the radiobuttonlist on post. The problem I’m having has to do with getting the values from the database mapped back to each radiobutton in the list. I simply don’t see how this is done using the activeRadiobuttonlist.

Thanks, Mike

I can see now, after a ton of frustration, that my database model won’t work with the activeRadiobuttonlist. What I need to do is to have a single column in the database that will hold an integer that will map to each of the different commenting options. Then I’ll need to create an enum and use it appropriately with queries or whatever.

1 => everyone to comment

2 => friends only

3 => disable commenting.

Mike

This is what I ended up with. Working now.


    <div class="row">

        <?php  echo CHtml::activeRadiobuttonlist($model, 'tok_commenting_option', array('1'=>'Allow everyone to comment.', '2'=>'Allow only friends to comment.', '3'=>'Allow no one to comment.'),array('class'=>'commenting_opts')); ?>

    </div>

tok_commenting_option is a property of the model and also a column in the database.

Spent way too much time on this one. Oh well…live and learn.

Cheers,

Mike