How to validate non model fileds


Here I have form to add the user credentials and addition to that module privilege also available.

How to set error message for privilege field label even though the privilege field name is not available in the user table.

Please help me…

Just add the validator to the list of validators in your model and assign the value to the object property, CActiveRecord uses a schema object to write into the DB so it knows the difference. I do however recommend you make use of a scenario so that other non form DB interactions don’t raise an error.

See my wiki article on scenarios:

Understanding Scenarios

Here’s an example:

Inside the model: (in this case access ‘name’ as $model->name)

public $name;

public function rules() {

  return array(

    array('name', 'required', 'on' => 'formValidation'), //pretend this is not in db



Outside the Model:

$model = new Login('formValidation');

if ($model->validate()) {



Obviously you will need to assign the data to the model somehow, which varies depending on whether you use CForm or CActiveForm or CHtml forms.