Question about CUniqueValidator

I’m trying to create registration feature, my model class extends CFormModel. I wanna use the unique validator to make sure the username doesn’t exist.

I tried looking in yii’s source code and found out that, to use the unique validator, I must provide the name of an active record class! Is it true? Is there any way to use the unique validator without having to use an active record?

Thanks in advanced,



I assume that you want to create a form where users are able to register their accounts (and therefore need unique usernames): So you would normally have a database table filled with user records (lets say it is called tbl_user). This would then be the data source for the uniqueValidator. As Yii uses the ActiveRecord approach you would create an Active Record modelclass called User for the table tbl_user and now you can use this modelclass for your validator (using the name ‘User’).

If you don’t want to use Active Record you could write your own validator that just runs a a simple SQL exists() statement. It is not that hard, you can look at the code of one of the validators to get an idea.