First off, I’d like to say this framework is the best thing since php5. I used to use cakephp but am now changing to yii, which I find waay better for my needs .
Anyways,
I came across this problem and I'm inquiring what the most optimal solution would be.
I have a basic database user login/logout/registration system.
I naturally added the uniqueValidator to the username field so that new users can only create accounts with unique usernames. Worked beautifully.
But now, the login action is broken. Why? Because the username that you would try to login with is obviously in the database already.
My first reaction was to try the ‘on’=>‘insert’ rule in the validation rules. But then I realized that as far as the login action can tell, the login action does use an insert quarry (the login form does not supply an pk ID, so it thinks it’s an insert).
What would be the best way to go about this problem?
<?php //My ruled as defined in the user model: public function rules() { return array( array('username, password', 'length', 'max'=>50), array('username', 'unique'), array('password', 'authenticatePass'), array('password_repeat', 'compare'), array('email', 'length', 'max'=>100), array('email', 'email'), array('username, password, group_id, email, password_repeat', 'required'), array('group_id', 'numerical', 'integerOnly'=>true), ); } //the login action public function actionLogin() { $user = new User; if (Yii::app()->request->isPostRequest) { // collect user input data if (isset($_POST['User'])) $user->setAttributes($_POST['User']); // validate user input and redirect to previous page if valid if ($user->validate(array('username', 'password', 'rememberMe')))// ; $this->redirect(Yii::app()->user->returnUrl); } // display the login form $this->render('login',array('user'=>$user)); } ?>
Feel free to criticize any of the above code btw, and in fact please do if you can.
Thanks and keep up the good work,
Jonah