Add additional error codes / error messages in LoginForm

I have an attribute "enabled" (boolean) in my User model.

How can I output a message such as "User account disabled" if enabled=0 ?

I am using CUserIdentity class with default LoginForm model.

You can check this in authenticate() and set $this->errorCode to desired message…

I need the same thing, but just setting $this->errorCode doesn’t show the message. Is there something else I need to set?

This is untested but it should work

In your loginform model


	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', 'skipOnError'=>true),

			// is the user enabled?

			array('enabled', 'checkEnabled', 'skipOnError'=>true,'enabledValue'=>1),

		);

	}


	/**

	 * Check to see if the user is enabled

	 * NOTE: this is untested!!!!

	 */

	public function checkEnabled($attribute,$params)

	{

		if(!$this->$attribute == $params['enabledValue'])

		{

			$this->addError('username','Sorry pal but you are not enabled');

		}	

	}

Let us know if it works!

doodle

As ahac said, your advice doesn’t work…

Temir

Wow… Where did you find this thread… the last post was made more than a year ago…

From your post I don’t understand if you wanted just to make a point on my post… or you need help with similar problem…

Hello,

Sorry to bother you with another late reply to this thread.

I just came a similar problem.

I have the class UserIdentity to authenticate username, password & check if the User is active upon login.

I added


if($user->active == 0)

	$this->errorCode = self::ERROR_USERNAME_NOT_ACTIVE;

to check if the user is active or not,

with


case UserIdentify::ERROR_USERNAME_NOT_ACTIVE:

       $this->addError('username','Username is currently not active, please activate using the activation URL in your email then retry again.');

in the LoginForm.php model.

however on the login page I get the following error in /var/logs/apache/error_log:

PHP Fatal error: Undefined class constant ‘ERROR_USERNAME_NOT_ACTIVE’ in /var/www/www/blabla/app/protected/components/UserIdentity.php on line 62, referer: http://blabla/index.php/site/login

The question is: how do I add this error handler ?

Thank you

Typo :).

UserIdent[color="#FF0000"]ity[/color] != UserIdent[color="#FF0000"]ify[/color]

Ah thank you ! however that did not solve the problem, same error msg :)

What if you check for a hardcoded number instead of constant? Still the same error?

I replcaed ERROR_USERNAME_NOT_ACTIVE with the number 3 in both files. That did it. Thank you sindwinder !

So you are halfway there. Now you should figure out why constant wasn’t working. You should always try and use variables over hardcoded values.

My personal bet is that ERROR_USERNAME_NOT_ACTIVE wasn’t declared public, but that’s just a wild guess :).

You are absolutly right !

I added const ERROR_USERNAME_NOT_ACTIVE = 3; to the UserIdentity class and TATAAAAA ! works like a charm now.

thank you so mush for your time and help ! :)