GSTAR
(Omzy83)
February 15, 2010, 12:42pm
1
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?
mdomba
(Maurizio Domba Cerin)
February 15, 2010, 12:54pm
2
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;
Onman
(O Rijkers)
February 15, 2010, 2:14pm
3
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;
GSTAR
(Omzy83)
February 15, 2010, 2:35pm
4
Onman:
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.
mdomba
(Maurizio Domba Cerin)
February 15, 2010, 2:39pm
5
I tryed it too and it’s working for me… is your password filed/attribute called password?
GSTAR
(Omzy83)
February 15, 2010, 3:02pm
6
Yes, it is called LoginForm[password]
In fact even when I submit the form with both fields blank only the username field gets highlighted.
Onman
(O Rijkers)
February 15, 2010, 6:05pm
7
GSTAR:
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";
mdomba
(Maurizio Domba Cerin)
February 16, 2010, 7:47am
8
Seems like you dont have this rule:
array('username, password', 'required'),
GSTAR
(Omzy83)
February 16, 2010, 9:40am
9
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
GSTAR
(Omzy83)
February 16, 2010, 9:57am
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?
mdomba
(Maurizio Domba Cerin)
February 16, 2010, 10:30am
11
change:
$record=User::model()->findByAttributes(array('email'=>$this->username));
to:
$record=User::model()->findByAttributes(array('email'=>$this->username,'enabled'=>1));
GSTAR
(Omzy83)
February 16, 2010, 10:53am
12
Onman:
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;
}
}
GSTAR
(Omzy83)
February 16, 2010, 10:56am
13
It’s working now, I had a problem with my CSS.
mdomba
(Maurizio Domba Cerin)
February 16, 2010, 11:04am
14
Bump… that’s right… should be only “default:”… I messed this one with copy-pasting…
GSTAR
(Omzy83)
February 16, 2010, 11:36am
15