login form in layout

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">

&lt;?php echo CHtml::activeLabel(&#036;model,'username'); ?&gt;


&lt;?php echo CHtml::activeTextField(&#036;model,'username') ?&gt;

</div>

<div class="row">

&lt;?php echo CHtml::activeLabel(&#036;model,'password'); ?&gt;


&lt;?php echo CHtml::activePasswordField(&#036;model,'password') ?&gt;

</div>

<div class="row rememberMe">

&lt;?php echo CHtml::activeCheckBox(&#036;model,'rememberMe'); ?&gt;


&lt;?php echo CHtml::activeLabel(&#036;model,'rememberMe'); ?&gt;

</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

Try to add this:




<?php echo CHtml::errorSummary($model); ?>



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(&#036;model);


Yii::app()-&gt;end();

}

// collect user input data

if(isset($_POST[‘LoginForm’]))

{

&#036;model-&gt;attributes=&#036;_POST['LoginForm'];


// validate user input and redirect to the previous page if valid


if(&#036;model-&gt;validate() &amp;&amp; &#036;model-&gt;login())


	&#036;this-&gt;redirect(Yii::app()-&gt;user-&gt;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).

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?

sure it is a special kind of widget

there is an extension called login portlet from where you could get inspiration:

http://www.yiiframework.com/extension/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!

:)

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.

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

glad that I could help!!

:)

regards!