emil_pgc
(Emil Thushara)
July 5, 2011, 9:53am
1
I want to create two login in my app
One will be the login which comes with every app
and second one i want to create should ask for only username and should login if the username is valid.
So i created a form login with a model class in which the username is stored.
now i want to know how to do the validation?
Can someone help me.
kiran123
(Sharmakiran71)
July 5, 2011, 10:01am
2
yes for this you can use same controller and model but just use different view file and another function in controller.
kiran123
(Sharmakiran71)
July 5, 2011, 10:18am
3
just do same code as you have done in UserIdentity.php file ( if you using that file for authentication )
and same code as original login function …
…
it takes little modification in code but most things remain same.
emil_pgc
(Emil Thushara)
July 5, 2011, 10:19am
4
Yes i have used the same model file… but different controller and diff view file…
Now i want to know the action to be given to validate it in controller
kiran123
(Sharmakiran71)
July 5, 2011, 10:24am
5
yes i am telling that function in controller will be mixture of UserIdentity file and your login function in controller.
emil_pgc
(Emil Thushara)
July 5, 2011, 10:26am
6
just do same code as you have done in UserIdentity.php file ( if you using that file for authentication )
and same code as original login function …
…
it takes little modification in code but most things remain same.
so are u saying to use the useridentity code inside my controller action?
like…
public function actionLogin()
{
$model=new PsmsUserAccInfo('login');
if(isset($_POST['PsmsUserAccInfo']))
{
$model->attributes=$_POST['PsmsUserAccInfo'];
if($model->validate())
{
$user=PsmsUserAccInfo::model()->findByAttributes(array('username'=>$this->username));
if ($user===null) { // No user found!
$this->_id='user Null';
$this->errorCode=self::ERROR_USERNAME_INVALID;
}
$this->_id = $user->id;
}
return !$this->errorCode;
}
return;
}
}
$this->render('login',array('model'=>$model));
}
kiran123
(Sharmakiran71)
July 5, 2011, 10:33am
7
you can use code like ,
public function yourNewFunction()
{
..
..
$record=Employee::model()->findByAttributes(array('E_EMAIL'=>$this->username));
..
..
if($record->username!== $this->username)
{
$this->_id=$this->username;
$this->errorCode=self::ERROR_PASSWORD_INVALID;
}
else if($record['STATUS']!=='Active') // extra code for user status
{
$err = "You have been Inactive by Admin.";
$this->errorCode = $err;
}
...
...
emil_pgc
(Emil Thushara)
July 5, 2011, 10:44am
8
public function actionLogin()
{
$model=new PsmsUserAccInfo('login');
if(isset($_POST['PsmsUserAccInfo']))
{
$model->attributes=$_POST['PsmsUserAccInfo'];
if($model->validate())
{
$user = PsmsUserAccInfo::model()->findByAttributes(array('username'=>$this->username));
if ($user->username!== $this->username)
{
$this->_id=$this->username;
$this->errorCode=self::ERROR_USERNAME_INVALID;
}
return;
}
}
$this->render('login',array('model'=>$model));
}
Property "PsmsEmpInfoController.username" is not defined.
kiran123
(Sharmakiran71)
July 5, 2011, 11:01am
9
please use,
print_r($_POST['PsmsUserAccInfo']);
and see what data posted.
emil_pgc
(Emil Thushara)
July 5, 2011, 11:05am
10
For checking i replaced the action with the above print command and got
Array ( [username] => emil ) as output
public function actionLogin()
{
$model=new PsmsUserAccInfo('login');
if(isset($_POST['PsmsUserAccInfo']))
{
$model->attributes=$_POST['PsmsUserAccInfo'];
if($model->validate())
{
$user = PsmsUserAccInfo::model()->findByAttributes(array('username'=>$this->username));
if ($user->username!== $this->username)
{
$this->_id=$this->username;
$this->errorCode=self::ERROR_USERNAME_INVALID;
}
return;
}
}
$this->render('login',array('model'=>$model));
}
Property "PsmsEmpInfoController.username" is not defined.
Where have you defined the actionLogin function? It seems you have defined it in PsmsEmpInfoController class.
Because of which
$user = PsmsUserAccInfo::model()->findByAttributes(array('username'=>$this->username));
gives the exception.
$this refers to the instance of the class in which the method is defined. You have got your code mixed up in the wrong classes
emil_pgc
(Emil Thushara)
July 5, 2011, 12:13pm
12
Where have you defined the actionLogin function? It seems you have defined it in PsmsEmpInfoController class.
Because of which
$user = PsmsUserAccInfo::model()->findByAttributes(array('username'=>$this->username));
gives the exception.
$this refers to the instance of the class in which the method is defined. You have got your code mixed up in the wrong classes
yes i have defined inside PsmsEmpInfoController… whr else should i define it??
emil_pgc
(Emil Thushara)
July 5, 2011, 12:48pm
13
Ok i changed my new action from PsmsEmpInfoController to siteController even when i know that there is gona be no big difference.
But now what i think is… i should make duplicate of LoginForm model and useridentity for my usage.
Is that the only way to proceed??
emil_pgc
(Emil Thushara)
July 6, 2011, 6:32am
14
Ok i changed my new action from PsmsEmpInfoController to siteController even when i know that there is gona be no big difference.
But now what i think is… i should make duplicate of LoginForm model and useridentity for my usage.
Is that the only way to proceed??
Guys can someone plssss help me…
zaccaria
(Matteo Falsitta)
July 6, 2011, 10:58am
15
You can use the same loginForm, just use a different scenario.
Take a look here about it.
emil_pgc
(Emil Thushara)
July 6, 2011, 11:02am
16
Yes i have made different login scenario form and connected with the userlogin db.
Chck my coding here
My Coding
zaccaria
(Matteo Falsitta)
July 6, 2011, 12:13pm
17
The error is:
if ($user->username!== $this->username)
It should be:
if ($user->username!== $model->username)
emil_pgc
(Emil Thushara)
July 6, 2011, 12:21pm
18
It’s Giving a error
[b]
Property "SiteController.username" is not defined. [/b]
zaccaria
(Matteo Falsitta)
July 6, 2011, 12:58pm
19
Also 2 lines forward there is:
$this->_id=$this->username;
The same stuff. $this in this case is the controller, the model is in $model, so wherever you have $this->username you have to change to $model->username.
emil_pgc
(Emil Thushara)
July 6, 2011, 1:09pm
20
zaccaria:
Also 2 lines forward there is:
$this->_id=$this->username;
The same stuff. $this in this case is the controller, the model is in $model, so wherever you have $this->username you have to change to $model->username.
After changing the coding to
if(isset($_POST['PsmsUserAccInfo']))
{
$model->attributes=$_POST['PsmsUserAccInfo'];
if($model->validate())
{
$user = PsmsUserAccInfo::model()->findByAttributes(array('username'=>$model->username));
print_r($user);
if ($user->username!== $model->username)
{
$model->_id=$model->username;
$this->errorCode=self::ERROR_USERNAME_INVALID;
}
return;
}
}
$this->render('login1',array('model'=>$model));
}
Error
[b] Property "PsmsUserAccInfo._id" is not defined.
[/b]