Yii Framework Forum: UserIdentity Error Messages - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

UserIdentity Error Messages Rate Topic: -----

#1 User is offline   GSTAR 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 1,003
  • Joined: 30-October 09
  • Location:UK

Posted 15 February 2010 - 07:42 AM

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?
0

#2 User is offline   Maurizio Domba Cerin 

  • Yii - Yesss It Is !!!
  • Yii
  • Group: Yii Dev Team
  • Posts: 4,317
  • Joined: 12-October 09
  • Location:Croatia

Posted 15 February 2010 - 07:54 AM

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;

Find more about me.... btw. Do you know your WAN IP?
1

#3 User is offline   Onman 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 175
  • Joined: 26-December 09
  • Location:The Netherlands

Posted 15 February 2010 - 09:14 AM

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;

0

#4 User is offline   GSTAR 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 1,003
  • Joined: 30-October 09
  • Location:UK

Posted 15 February 2010 - 09:35 AM

View PostOnman, on 15 February 2010 - 09:14 AM, said:

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.
0

#5 User is offline   Maurizio Domba Cerin 

  • Yii - Yesss It Is !!!
  • Yii
  • Group: Yii Dev Team
  • Posts: 4,317
  • Joined: 12-October 09
  • Location:Croatia

Posted 15 February 2010 - 09:39 AM

View PostGSTAR, on 15 February 2010 - 09:35 AM, said:

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?
Find more about me.... btw. Do you know your WAN IP?
0

#6 User is offline   GSTAR 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 1,003
  • Joined: 30-October 09
  • Location:UK

Posted 15 February 2010 - 10:02 AM

Yes, it is called LoginForm[password]

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

#7 User is offline   Onman 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 175
  • Joined: 26-December 09
  • Location:The Netherlands

Posted 15 February 2010 - 01:05 PM

View PostGSTAR, on 15 February 2010 - 10:02 AM, said:

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";
0

#8 User is offline   Maurizio Domba Cerin 

  • Yii - Yesss It Is !!!
  • Yii
  • Group: Yii Dev Team
  • Posts: 4,317
  • Joined: 12-October 09
  • Location:Croatia

Posted 16 February 2010 - 02:47 AM

View PostGSTAR, on 15 February 2010 - 10:02 AM, said:

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


Seems like you dont have this rule:
array('username, password', 'required'),

Find more about me.... btw. Do you know your WAN IP?
0

#9 User is offline   GSTAR 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 1,003
  • Joined: 30-October 09
  • Location:UK

Posted 16 February 2010 - 04:40 AM

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
0

#10 User is offline   GSTAR 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 1,003
  • Joined: 30-October 09
  • Location:UK

Posted 16 February 2010 - 04:57 AM

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

#11 User is offline   Maurizio Domba Cerin 

  • Yii - Yesss It Is !!!
  • Yii
  • Group: Yii Dev Team
  • Posts: 4,317
  • Joined: 12-October 09
  • Location:Croatia

Posted 16 February 2010 - 05:30 AM

View PostGSTAR, on 16 February 2010 - 04:57 AM, said:

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));



Find more about me.... btw. Do you know your WAN IP?
1

#12 User is offline   GSTAR 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 1,003
  • Joined: 30-October 09
  • Location:UK

Posted 16 February 2010 - 05:53 AM

View PostOnman, on 15 February 2010 - 09:14 AM, said:

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;


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;
    }
}

0

#13 User is offline   GSTAR 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 1,003
  • Joined: 30-October 09
  • Location:UK

Posted 16 February 2010 - 05:56 AM

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

#14 User is offline   Maurizio Domba Cerin 

  • Yii - Yesss It Is !!!
  • Yii
  • Group: Yii Dev Team
  • Posts: 4,317
  • Joined: 12-October 09
  • Location:Croatia

Posted 16 February 2010 - 06:04 AM

View PostGSTAR, on 16 February 2010 - 05:53 AM, said:

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:".


Bump... that's right... should be only "default:"... I messed this one with copy-pasting...
Find more about me.... btw. Do you know your WAN IP?
0

#15 User is offline   GSTAR 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 1,003
  • Joined: 30-October 09
  • Location:UK

Posted 16 February 2010 - 06:36 AM

No prob mdomba :)

Can you help me with this: http://www.yiiframew...t-login-expiry/
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users