Yii Framework Forum: login form in layout - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

login form in layout how can i add a login form in each page? Rate Topic: -----

#1 User is offline   daniele 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 24
  • Joined: 04-June 10

Posted 04 June 2010 - 05:07 AM

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
0

#2 User is offline   zitter 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 140
  • Joined: 14-July 09

Posted 04 June 2010 - 09:29 AM

Try to add this:
<?php echo CHtml::errorSummary($model); ?>

0

#3 User is offline   daniele 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 24
  • Joined: 04-June 10

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).
0

#4 User is offline   Junior - df9 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 416
  • Joined: 24-May 09
  • Location:Brazil

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!
0

#5 User is offline   daniele 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 24
  • Joined: 04-June 10

Posted 04 June 2010 - 10:24 AM

View Postscoob.junior, on 04 June 2010 - 10:18 AM, said:

what about creating a portlet for this?


Uhmm can you please tell me where can i find documentation about creating a portlet?
A portlet is a kind of widget isn'it?
0

#6 User is offline   Junior - df9 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 416
  • Joined: 24-May 09
  • Location:Brazil

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
<?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!
0

#7 User is offline   daniele 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 24
  • Joined: 04-June 10

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.
0

#8 User is offline   daniele 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 24
  • Joined: 04-June 10

Posted 04 June 2010 - 11:09 AM

OK!!! It seems to work, Thanks a lot again
0

#9 User is offline   Junior - df9 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 416
  • Joined: 24-May 09
  • Location:Brazil

Posted 04 June 2010 - 11:19 AM

View Postdaniele, 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!
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users