Check If Username Exists Doesnt Work

In the code below I’m working on a username validation.

I’d think that this would always throw an error, but it doesnt do anything.

How come it doesnt reach validateUsername() ?


class RegisterForm extends CFormModel

{

        public $email;

        public $username;

        public $password;

        public $password2;

        

        private $_identity;

 


        public function rules()

        {

                return array(

                        array('username', 'validateUsername'),

                        array('email, username, password, password2', 'required'),

                        array('password2', 'compare', 'compareAttribute' => 'password'),

                );

        }

    

        public function validateUsername()

        {

           $this->addError('username','Username already exists.');

        }

}

The validation method needs to be called:


public function validatorUsername($attribute, $params) {

And the validation rule needs to be called:


array('username', 'username'),

Read more info on rules() method.

Dear Friend

I hope this can be done in 2 ways.

1.custom validation as you have tried.




class RegisterForm extends CFormModel

{

        public $email;

        public $username;

        public $password;

        public $password2;

        

        private $_identity;

 


        public function rules()

        {

                return array(

                        array('username', 'validateUsername'),

                        array('email, username, password, password2', 'required'),

                        array('password2', 'compare', 'compareAttribute' => 'password'),

                );

        }

    

        public function validateUsername($attribute,$params)

        {  if(User::model()->exists('username=:username',array('username'=>$this->username))

             $this->addError('username','Username already exists.');

        }

}




2.Using CUniqueValidator




class RegisterForm extends CFormModel

{

        public $email;

        public $username;

        public $password;

        public $password2;

        

        private $_identity;

 


        public function rules()

        {

                return array(

                        array('username', 'unique','className'=>'User','attributeName'=>'username','message'=>"Username already exists"),

                        array('email, username, password, password2', 'required'),

                        array('password2', 'compare', 'compareAttribute' => 'password'),

                );

        }

    

        

}




Regards.

Note: Initially I have wrongly mentioned about the CExistValidator. Sorry for that.

you can make use of unique validator

@seenivasan

I tried both your solutions but it’s still possible to register with the same username.

The validateUsername($attribute,$params) isnt being called and the unique validator is being ignored

It seems that both your answers are correct according to the yii guide so its weird I can still register as the same username over and over again

Could something in my config.php be turned on/off ?

Are you using Save() or Validate()?

I turned on enableAjaxValidation and it works now :)


<?php $form=$this->beginWidget('CActiveForm', array(

        'id'=>'register-form',

	'enableClientValidation'=>true,

    [b]'enableAjaxValidation' => true,[/b]

	'clientOptions'=>array(

		'validateOnSubmit'=>true,

	),

What if the user has javascript turned off?

Validation should work with or without.

I didnt have the $model->validate() in the actionRegister method.

When I put that it the whole thing works, even without javascript turned on.