Yii 1.1: Working with radiobutton list


This is a tutorial to work with radioButtonList which will handle enum datatype. Form design and validation both are detailed here.

Try this and let me know if here is any issue or any other better way to do this.


Let we have enum for table field menuType as shown below.

  • menuType : enum('Page','PhpPage','External')

  • pageId: int (If menuType is 'Page')

  • phpPageId: int (If menuType is 'PhpPage')

  • externalLink: varchar (If menuType is 'External')

Form elements:

Now in form, based on menu type selection i want to show or hide other 3 fields. The below code will take care of it. I have called 'menuTypeChange' function on change of any radio selection. This function will show/hide respected elements.

<script language="javascript">
function menuTypeChange(menyType)
<div class="row">
        <?php echo $form->labelEx($model,'menuType'); ?>
        <?php echo $form->radioButtonList($model,'menuType',array('Page'=>'Page','PhpPage'=>'PHP Page', 'External'=>'External'), array('onchange' => 'menuTypeChange(this.value);')); ?>
        <?php echo $form->error($model,'menuType'); ?>
    <div id="PageDiv" class="row menuType">
        <?php echo $form->labelEx($model,'pageId'); ?>
        <?php echo $form->dropDownList($model, 'pageId', CHtml::listData(Page::model()->findAll(array('condition'=>'isDeleted=:isDeleted', 'params'=>array(':isDeleted'=>0))), 'pageId', 'pageName'), array('empty'=>'- - Select - -')); ?>      
        <?php echo $form->error($model,'pageId'); ?>
    <div id="PhpPageDiv" class="row menuType">
        <?php echo $form->labelEx($model,'phpPageId'); ?>
        <?php echo $form->dropDownList($model, 'phpPageId', CHtml::listData(PhpPage::model()->findAll(), 'phpPageId', 'phpPageName'), array('empty'=>'- - Select - -')); ?>      
        <?php echo $form->error($model,'phpPageId'); ?>
    <div id="ExternalDiv" class="row menuType">
        <?php echo $form->labelEx($model,'externalLink'); ?>
        <?php echo $form->textField($model,'externalLink',array('size'=>60,'maxlength'=>255)); ?>
        <?php echo $form->error($model,'externalLink'); ?>
<script language="javascript">
    menuTypeChange('<?php echo $model->menuType;?>');

I have done with form. Now the problem is validation.

How validation works?

With default rules when i submit form it is asking me all 3 fields validation even i have selected only one menu type. For this let me create one new custom validation function as shown below.

public function rules()
  return array(
   array('menuType', 'required'),
    array('externalLink', 'url'),
public function radioValidate($attribute,$params)
    $field = $params[0];
    $value = $params[1];
    if($this->$field == $value && $this->$attribute == '')
        $this->addError($attribute,$this->getAttributeLabel($attribute).' cannot be blank.');

Let me explain more on this rule:


Above rule means: If menuType's value is Page then only pageId is compulsory.

Same goes with other too,


Total 3 comments

#18320 report it
SomethingWicked at 2014/10/15 05:20am

To solve the validation problem I did the following:

defined a jquery function:

$.fn.menuTypeChange = function() { ... };

and called this way:

array('onchange' => '$(this).menuTypeChange();')

instead of:

array('onchange' => 'menuTypeChange(this.value);')

#17281 report it
manojvelyalan at 2014/05/19 10:51am
Client side validation

client side validation is not working..

#14777 report it
MaziTizeh at 2013/09/09 05:40am

Hi, Great wiki , but validation is not working for me, same i copy paste it

Advise plz

Leave a comment

Please to leave your comment.

Write new article