Validate Field That Is Not In Model


I have the following issue that I cannot solve and also cannot find any solution in this forum (yet)

On my page there is an input form with username + group

Both fields are select-lists that I create using CActiveForm dropDownList() method

The group list has an Item called "add new", when this item is selected a new input is displayed (created with CHtml::textField())

This new textField that is displayed to enter a new group-name is not in database

The desired logic is, that I can add validation to my CActiveRecord to validate that group_id is greater than 0 OR group_name is string longer than 3 characters.

In my database table there are only the two fields user_id and group_id. group_name is not in the table which is linked to CActiveRecord

What I do is, that onBeforeSave I check: If the group_id is 0 and group_name has a value then

  • Add the group (using a different model)

  • Read the new group_id and use it

The MAIN problem now is, that there never is an attribute for group_name created in my model.

I cannot validate or process that value.

Possibly there is a way to tell the model, that an attribute can be validated without requiring an database-field

(something like capcha can be validated even though it is not in the database, but I cannot figure out how!)

Thanks for your help


you can declare an attribute in your model then use it just like database fields so you can validate or assign value to it

class MyModel extends CActiveRecord


    public $group_name;




Thank you, this is working!

One mistake I made was to check "$this->attributes" for the group name.

This array does NOT contain my user-defined attribute "group_name", so I did assume that it was ignored

So the group_name is available directly as $this->group_name

Btw. Also the Ajax-validation using some validators in rules() works perfectly now :slight_smile:

Note: Here I find more details on that topic: