How Do I Show “Invalid Username” Instead Of “Invalid Username/password” When Username Is Invalid

Plz look at below code. This always shows "invalid username" instead of "invalid username/password" even when username is invalid. How do i make it show error as "invalid username".

I am specifically looking at how yii is displaying this as error i.e This thing shows up equivalent to a model error just below the field


sitecontroller

    public function actionLogin()

    {

        $model=new LoginForm;




        // collect user input data

        if(isset($_POST['LoginForm']))

        {

            $model->attributes=$_POST['LoginForm'];

            // validate user input and redirect to the previous page if valid

            if($model->validate() && $model->login()){

                // loggedin 

            }

        }

        // display the login form

        $this->render('login',array('model'=>$model));

    }

User Identity.php

        public function authenticate()

        {

                $record = User::model()->with('UserType')->findByAttributes(array('username'=>$this->username));

                if($record===null){

                        $this->errorCode = self::ERROR_USERNAME_INVALID;

                }

                else if($record->password!==sha1($this->password)){

                        $this->errorCode = self::ERROR_PASSWORD_INVALID;       

                }

                else

                {

                        $this->_id=$record->id;

                        $this->setState('role', $record->UserType->name);

                        $this->errorCode=self::ERROR_NONE;

                }

                return !$this->errorCode;

        }


LoginForm.php

        public function login()

        {

                if($this->_identity===null)

                {

                        $this->_identity=new UserIdentity($this->username,$this->password);

                        $this->_identity->authenticate();

                }

                if($this->_identity->errorCode===UserIdentity::ERROR_NONE)

                {

                        $duration=$this->rememberMe ? 3600*24*30 : 0; // 30 days

                        Yii::app()->user->login($this->_identity,$duration);

                        return true;

                }

                else

                        return false;

        }

This is more or less the default code that yii provides

Dear Friend

By default the custom validator authenticate the attribute passworrd not the username.

LoginForm.php




public function rules()

	{

		return array(

			// username and password are required

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

			// rememberMe needs to be a boolean

			array('rememberMe', 'boolean'),

			// password needs to be authenticated

			array('password', 'authenticate'),

			

		);

	}


public function authenticate($attribute,$params)

	{

		if(!$this->hasErrors())

		{

			$this->_identity=new UserIdentity($this->username,$this->password);

			if(!$this->_identity->authenticate()){

				$this->addError('password','Incorrect username or password.');

		

		}

	}



If you want to display the error under username when username is invalid.

We can do the following.

LoginForm.php




public function rules()

	{

		return array(

			// username and password are required

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

			// rememberMe needs to be a boolean

			array('rememberMe', 'boolean'),

			// username and password need to be authenticated

			array('password,username', 'authenticate'),

			

		);

	}


public function authenticate($attribute,$params)

	{

		if(!$this->hasErrors())

		{

			$this->_identity=new UserIdentity($this->username,$this->password);

			if(!$this->_identity->authenticate())

                        {		

			    if($this->_identity->errorCode==UserIdentity::ERROR_USERNAME_INVALID)

				$this->addError('username','Incorrect username.');	

			    if($this->_identity->errorCode==UserIdentity::ERROR_PASSWORD_INVALID)

				$this->addError('password','Incorrect password.');

                        }

		}

	}



In the above scenario, if both username and password are invalid,we are going to get error display for only username.

Regards.

I already figured it out anyways…thanks