Cactiveform::dropdownlist + 'default' Validation Rule Bug

Hi Everybody,

I guess I found small bug connected to forms, dropdown lists and model validation.

How to reproduce this?

  1. Create simple model with two properties, e.g. like below:

// class Example extends CActiveRecord

public function rules()


    return array(

        array('name', 'length', 'max' => 20),

        array('country_id', 'default', 'value' => new CDbExpression('NULL')),



  1. Create simple form, without ajax validation:

/* @var $form   CActiveForm */

/* @var $model  Example */

/* @var $data   array */

echo $form->textField($model, 'name');

echo $form->error($model, 'name');

echo $form->dropDownList($model, 'country_id', $data);

echo $form->error($model, 'country_id');

Let’s suppose that controller is set up properly - it sets all properties to object of Example class and calls save() method on it. On validation errors, it renders form view again with error messages.

Everything is fine so far.

Now, enter text with length above 20 characters into ‘name’ field, leave ‘country_id’ empty and submit your form. Application will crash with this error message:

Property "CDbExpression.primaryKey" is not defined. 

This is because value of ‘country_id’ is after validation set to new CDbExpression(‘NULL’), which cannot be used as selected item of dropdown.

Solution is quite simple. Inside CHtml::activeDropDownList I changed:

// this line


// into this


Additional information:

  • operating system - Windows 7 64bit

  • Web server - Apache on Xampp

  • browser type - Firefox 17.0.1

  • Yii version - 1.1.12



Thanks, this was very helpful!