HELP ON ACTIVE RADIOBUTTONLIST

<?php

$accountStatus = array(‘Active’=>‘Active’, ‘Disabled’=>‘Disabled’);

echo CHtml::activeRadioButtonList($user,‘AccountStatus’,$accountStatus,array(‘separator’=>’ '));

?>

anybody knows how will i set the select radio button when i run this code? AccounStatus are fetch on a database with a value either 0 or 1 which is active and disabled. Please help…tnx

I ran into a similar problem. I’ll share how I got around it but there may be a much better way of dealing with this issue.

First you need to convert the data when it is retrieved in order to have it in the format that the button will expect. In your case it looks like you need to convert the database’s native ‘0’ and ‘1’ value into ‘Active’ and ‘Disabled’ respectively. Then after the form data is read you’ll actually need to convert those values back. These functions would be added to your user model class (whichever class $user) represents in your example.

You could do this manually in the controller, but I chose to do so in the actual AR model by extending the afterFind() and beforeSave() methods.

this is what it might look like for you:


protected afterFind()

{

 parent::afterFind();


  if($this->AccountStatus == '1')

   $this->AccountStatus = 'Active';

 else

  $this->AccountStatus = 'Disabled';

}

and




 protected beforeSave()

 {

  if($this->AccountStatus == 'Active')

   $this->AccountStatus = '1';

  else

   $this->AccountStatus = '0';


  parent::beforeSave();

 }



If it turns out that your db is actually returning true and false values simply replace ‘0’ with false and ‘1’ with true. You’ll need to always convert AccountStatus into the format that your form is expecting before rendering so if you have a form where the data is repopulated after you save you’ll also need to add the same code to to the afterSave() method that you added to the afterFind() method.

Not sure if this is the best way of doing things but it does work. Hope it helps!!! And if anyone has a better way I’d sure love to see it. ::)

$accountStatus = array(1=>‘Active’, 0=>‘Disabled’);

See, that didn’t actually work for me. Our database populates the attribute as being true if true in the db, and false if false.

So, say that you have a AR class called Person with an attribute called isSane that is Boolean in the db (I’m using postgre). Then after loading the model by say:

$p = Person::model()->findByPk(1);

$p->isSane = true or false (depending on the value in the db).

So, I tried array(1=>‘Sane’, 0=>‘Crazy’) and that doesn’t seem to work, nor did

array(true=>‘Sane’, false=>‘Crazy’) (yes, I actually tried this JUST to see if it would work…I’m not so proud about that).