validation rule in a model

Hi all,

I have a model in which I have array of validation rules.

I add in those rules, a specific rule and I provide the function which does this verification.

My function returns true in case of it is ok and false in case it is not ok.

However when I validate the model, the validation rule is always OK whatever the test I do. It seems that returning true or false has no effect on the validation of the model.

	public function rules()


		// NOTE: you should only define rules for those attributes that

		// will receive user inputs.

		return array(

			array('email, password', 'required','on'=>'login,modify'),


			//array('is_active', 'numerical', 'integerOnly'=>true),

			array('email', 'length', 'max'=>100),

            array('email', 'email'),


			array('password', 'length', 'max'=>300),

            array('amaCode', 'length', 'max'=>15),

            array('password', 'compare', 'compareAttribute'=>'password_repeat','on'=>'register,modify'),

            array('amaCode', 'validateRegister', 'on'=>'register'),





AND THE FUNCTION is only intended to compare the code given in a form to the code in the database

    public function validateRegister($attribute,$params) {



        $query = Yii::app()->db->createCommand()






        // Comparer le code ama à celui en database

        if(isset($_POST['User'])) {




           if ($this->amaCode === $query['code_ama']) {

               return true;



               return false;





What is the reason why whatever the code I give in the form (same or not the same compared to the one in the database) the validate() function always return OK


What is model->attributes? You haven’t declared it in the function, you’re referencing an uninitialized variable. Is this causing your problem? Did you intend to write


Hi Luke Jurgs, thanks to your reply

Unfornutely I replace $model-> by $this-> and the symptom is the same. Always pass Ok the validate

To be more precise I wrote and it is the same result:

public function validateRegister($attribute,$params) {


        return false;



Hi ajaxian,

You have to use CModel::addError() on validation failure, as you see in the following section of the guide.

Declaring Validation Rules … see "authenticate" function

So, maybe:

public function validateRegister($attribute,$params) {

        $query = Yii::app()->db->createCommand()




       if ($this->amaCode !== $query['code_ama']) {

           $this->addError("amaCode", "Incorrect AMA Code");



BTW, I don’t understand the following lines of your code:

I don’t think it’s a good practice to refer $_POST directly from a model class method …

Anyway, probably $_POST[‘User’] is always undefined every time the function is called.

Otherwise "$model->attributes" should have fired an exception to break the program.

Hi and thanks softark,

Right ! I wrote lines of code I don’t understand myself when re-reading.

Anyway I focus only on the mechanism behind the validate.

I obtain the same result when just writing

return false;

in the validate method. That’s the reason of my trouble

OK. I see.

So, do nothing on success and addError() on failure, instead of returning true or false. :)

Aaaarrggghh !

It works know with your explaination @softark

Thanks all for your help