UserIdentity Error Messages

The error messages generated by the UserIdentity login form makes it obvious what the incorrect value is, for example:


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

        if($record===null)

            $this->errorCode=self::ERROR_USERNAME_INVALID;

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

            $this->errorCode=self::ERROR_PASSWORD_INVALID;

Instead I want to have a general message that says "Incorrect login details". How can I do this?

You can change the code that sets the message to be displayed… in the models/LoginForm.php

change:




...

                case UserIdentity::ERROR_USERNAME_INVALID:

                    $this->addError('username','Username is incorrect.');

                    break;

                default: // UserIdentity::ERROR_PASSWORD_INVALID

                    $this->addError('password','Password is incorrect.');

                    break;



to




                case UserIdentity::ERROR_USERNAME_INVALID:

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

                    break;

                default: // UserIdentity::ERROR_PASSWORD_INVALID

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

                    break;



If you want both the username and the password highlighted when either one is wrong, try




	case UserIdentity::ERROR_USERNAME_INVALID:

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

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

		break;

	case default:

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

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

		break;



That does not work for me I’m afraid - the password field never gets highlighted. It would be useful to have this working.

I tryed it too and it’s working for me… is your password filed/attribute called password?

Yes, it is called LoginForm[password]

In fact even when I submit the form with both fields blank only the username field gets highlighted.

Can you try assigning the password a default value, e.g. $form->password = "XXX";

Seems like you dont have this rule:




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



mdomba - I already have that rule in my model

Onman - I’m not sure how to do that but in my model I set public $password=“XXX”; but that did not make any difference either.

By the way I’m using version 1.0.10

Also I have an "enabled" field in my users table - how can I ensure only users with enabled value of 1 can log in?

change:




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



to:




$record=User::model()->findByAttributes(array('email'=>$this->username,'enabled'=>1));



By the way in that above code you have "case default:" but that is not a valid statement (gives an error), instead I used just "default:".

Also here is my class UserIdentity:


class UserIdentity extends CUserIdentity

{

    private $_id;

    public function authenticate()

    {

        $record=User::model()->findByAttributes(array('email'=>$this->username, 'enabled'=>1));

        if($record===null)

            $this->errorCode=self::ERROR_USERNAME_INVALID;

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

            $this->errorCode=self::ERROR_PASSWORD_INVALID;

        else

        {

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

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

            $this->errorCode=self::ERROR_NONE;

        }

        return !$this->errorCode;

    }

 

    public function getId()

    {

        return $this->_id;

    }

}

It’s working now, I had a problem with my CSS.

Bump… that’s right… should be only “default:”… I messed this one with copy-pasting…

No prob mdomba :)

Can you help me with this: http://www.yiiframework.com/forum/index.php?/topic/7251-inconsistent-login-expiry/