Hi all,
the question is: how can i put a login form in every page of my website and let the validation happen in the layout itself?
I tried to create a LoginForm model as written in the documentation, i modified the UserIdentity component to check in the database for authentication and i create a form in the layout as below:
if (!isset ($model)) $model= new LoginForm();
echo CHtml::beginForm(array('site/login'));?>
<div class="row">
<?php echo CHtml::activeLabel($model,'username'); ?>
<?php echo CHtml::activeTextField($model,'username') ?>
</div>
<div class="row">
<?php echo CHtml::activeLabel($model,'password'); ?>
<?php echo CHtml::activePasswordField($model,'password') ?>
</div>
<div class="row rememberMe">
<?php echo CHtml::activeCheckBox($model,'rememberMe'); ?>
<?php echo CHtml::activeLabel($model,'rememberMe'); ?>
</div>
<div class="row submit">
<?php echo CHtml::submitButton('Login',array('class'=>'art-button')); ?>
</div>
<?php echo CHtml::endForm();?>
In the site/controller i created correctly an actionLogin that perform validation but now i don't know how can i render messages/error in the login form itself! It's placed in the layout i mean and the controller can render just its own view and i don't want to create a login view that duplicate the form, in the login page.
Thanks in advance for reply
Page 1 of 1
login form in layout how can i add a login form in each page?
#3
Posted 04 June 2010 - 10:01 AM
Uhmm nothing: here is my code in sitecontroller..
public function actionLogin(){
$model=new LoginForm;
// if it is ajax validation request
if(isset($_POST['ajax']) && $_POST['ajax']==='login-form')
{
echo CActiveForm::validate($model);
Yii::app()->end();
}
// collect user input data
if(isset($_POST['LoginForm']))
{
$model->attributes=$_POST['LoginForm'];
// validate user input and redirect to the previous page if valid
if($model->validate() && $model->login())
$this->redirect(Yii::app()->user->returnUrl);
}
// display the login form
$this->render('login',array('model'=>$model));
}
As i think it should be, i render a blank "login" view with $model as parameter; this parameter should be evalueted by <?php echo CHtml::errorSummary($model); ?> in the main.php layout file, but it doesn't happen.
Obviously, if a write the same login form in the login.php view everything goes fine and the messages appear.
Please help me, i Know i probably miss something stupid (it's the first time i try to do something with yii).
public function actionLogin(){
$model=new LoginForm;
// if it is ajax validation request
if(isset($_POST['ajax']) && $_POST['ajax']==='login-form')
{
echo CActiveForm::validate($model);
Yii::app()->end();
}
// collect user input data
if(isset($_POST['LoginForm']))
{
$model->attributes=$_POST['LoginForm'];
// validate user input and redirect to the previous page if valid
if($model->validate() && $model->login())
$this->redirect(Yii::app()->user->returnUrl);
}
// display the login form
$this->render('login',array('model'=>$model));
}
As i think it should be, i render a blank "login" view with $model as parameter; this parameter should be evalueted by <?php echo CHtml::errorSummary($model); ?> in the main.php layout file, but it doesn't happen.
Obviously, if a write the same login form in the login.php view everything goes fine and the messages appear.
Please help me, i Know i probably miss something stupid (it's the first time i try to do something with yii).
#4
Posted 04 June 2010 - 10:18 AM
what about creating a portlet for this?
______________________________________
Junior
df9.com.br
Linux Registered User #364954
GNU/Linux: together we're ready!
Junior
df9.com.br
Linux Registered User #364954
GNU/Linux: together we're ready!
#6
Posted 04 June 2010 - 10:44 AM
sure it is a special kind of widget
there is an extension called login portlet from where you could get inspiration:
http://www.yiiframew...xtension/login/
also, in the blog tutorial there are some examples, like usermenu, tag cloud and recent comments
http://www.yiiframework.com/doc/
basically all you have to do is to create a new class file under protected/Components and inherit from CPortlet, overwrite one or two methods (like init() or renderContent()) and create a view file under Components/views, which will display the HTML content
see:
/protected/Components/MyPortlet.php
/protected/Components/views/myPortletView.php
hope it helps!
there is an extension called login portlet from where you could get inspiration:
http://www.yiiframew...xtension/login/
also, in the blog tutorial there are some examples, like usermenu, tag cloud and recent comments
http://www.yiiframework.com/doc/
basically all you have to do is to create a new class file under protected/Components and inherit from CPortlet, overwrite one or two methods (like init() or renderContent()) and create a view file under Components/views, which will display the HTML content
see:
/protected/Components/MyPortlet.php
<?php
Yii::import('zii.widgets.CPortlet');
class MyPortlet extends CPortlet{
public function renderContent(){
$this->render('myPortletView',array('myvar'=>'hello!!'));
}
}
?>
/protected/Components/views/myPortletView.php
<?php echo $myvar; ?>
hope it helps!
______________________________________
Junior
df9.com.br
Linux Registered User #364954
GNU/Linux: together we're ready!
Junior
df9.com.br
Linux Registered User #364954
GNU/Linux: together we're ready!
#7
Posted 04 June 2010 - 10:55 AM
Ok i try this way that seems also the most portable and reusable
Thanks a lot for the hint, i'll let you know if i'll solve this problem.
Thanks a lot for the hint, i'll let you know if i'll solve this problem.
#9
Posted 04 June 2010 - 11:19 AM
daniele, on 04 June 2010 - 11:09 AM, said:
OK!!! It seems to work, Thanks a lot again
glad that I could help!!
regards!
______________________________________
Junior
df9.com.br
Linux Registered User #364954
GNU/Linux: together we're ready!
Junior
df9.com.br
Linux Registered User #364954
GNU/Linux: together we're ready!
Share this topic:
Page 1 of 1

Help












