Jump to content
Larry Ullman's Book Forums

Using Login Model On Other Pages


Recommended Posts

Hi,

 

I have a working login form. But I also have a little drop down login in my nav bar. I'm currently trying to create the form properly through Yii using active fields.

 

I'm a little unsure of how to do this though. In particular of passing the LoginForm model to the pages.

 

I have tried in my SiteController actionIndex() 

 

 
// Login Form model for nav bar
$loginModel = new LoginForm();
 
// render index view
$this->render('index',array(
           // other models
'loginModel'=>$loginModel,
        ));

 

And then tried to use it in the layout file like so:

 

 

 
 
 echo CHtml::beginForm(array('site/login'), 'post'); ?>
 
                            <?php echo CHtml::activeTextField($loginModel, 'username', '', array('placeholder'=>'Your Email')); ?>
                            <?php echo CHtml::activePasswordField($loginModel,'password', '', array('placeholder'=>'Password')); ?>
 
// rest of form 

 

But it always returns undefined.

 

Where am I going wrong?

 

Thanks

 

Jonathon

Link to post
Share on other sites

Okay. Well, only the $content variable gets passed to the layout page. To pass the $loginModel, you could add it as an attribute to components/Controller.php, just like the breadcrumbs and menu items. Then it'll be available in the layout file via $this->loginModel. You'll need to assign it a value (of new LoginForm) at some point. Let me know if you need help with that.

  • Upvote 1
Link to post
Share on other sites

Hi Larry,

 

Thanks for that, I'll take a look and see how I get on. On a related note, I feel like I may be using layouts incorrectly in that I have 3 different layouts. But each time I replicate the code for the nav bar and footer. What's the way round this?

Link to post
Share on other sites

Hi Larry,

 

Thanks for your help, this is how I did this for anyone interested.

 

Controller.php

 

 

 
/**
* @var object of LoginForm() type. For use with passing loginForms to all pages
*/
 
public $loginModel;

SiteController.php

 

		// Login Form model for nav bar
		$loginModel = new LoginForm();
		
		$this->loginModel = $loginModel;

 

Layout.php

 

                            <?php echo CHtml::activeTextField($this->loginModel, 'email', array('placeholder'=>'Your Email', 'class'=>'drop-login')); ?>
                            <?php echo CHtml::activePasswordField($this->loginModel, 'password', array('placeholder'=>'Password', 'class'=>'drop-login')); ?>

 

Thanks for all your help, although I am still wondering about the layout  question I posed in my previous post.

 

Thanks

 

Jonathon

Link to post
Share on other sites

Hi Larry,

 

Thatnks for the pointer with layouts. I think i've sorted this now. However I've run into another problem with a drop down form in my nav bar. I placed the code in the actionIndex() method of the site controller. But when I use another controller say Area I get the following error

 

 

Fatal error: Call to a member function getValidators() on a non-object in C:\xampp\yii-1-1-12\framework\web\helpers\CHtml.php

 

I'm assuming because I explored this problem that because my code to pass the loginModel is only prevalent in the SiteController's actionIndex() method that I can't access it when another controller is in use. Which makes sense. The error seems to occur from the line

 

 

<?php echo CHtml::activeTextField($this->loginModel, 'email', array('placeholder'=>'Your Email', 'class'=>'drop-login')); ?> 

Which is how I've come to this conclusion. as when I comment it out the page loads.

 

But I'm not sure how to make it available to every controller without creating redundancies. I have 

 

 

 
/**
* @var object of LoginForm() type. For use with passing loginForms to all pages
*/
 
public $loginModel;
 

 

in my Controller.php, but you said  I need to assign $loginModel a value (of new LoginForm) at some point. I'm unsure where to do this though so it's available to all controllers?

 

Jonathon

Link to post
Share on other sites

Thank you Larry.

 

I tried to use a constructor and this actually fails in that in creates the error: SiteController cannot find the requested view.

 

(Replace controller and view with whatever view/controller you're after depending on the page accessed).

 

Another thought struck me though. I had planned to use SSL on Login, logout, verify, register, change pass and forgot pass pages. Now I don't know your thoughts on that as it is. I just read an article sometime ago. But having a drop down login box in my navigation bar would therefore require me to make the whole site HTTPS I would think. Which is something I asked on here a while ago and generally peoples thoughts were not to do that.

Link to post
Share on other sites

I'm not sure that I follow that error message as a result. I can mock this up myself and see if it works as I intended, if you'd like. 

 

But, yes, if your login form requires SSL, then either every page would need to be HTTPS (which you probably don't want to do), or you'd need to use an iframe, which creates its own problems. But whether or not your login form needs SSL depends upon the application.

Link to post
Share on other sites

Hi Larry,

 

Yes it was a weird message, I didn't see how it related. Here is some of the stack. 

 

"throw new CException(Yii::t('yii','{controller} cannot find the requested view "{view}".',"

 

If you want to do that feel free to, but don't feel obligated. It seems to be generated by just calling a __construct() inside Controller.php.

 

What would your thoughts be on when a login, change password etc would need SSL? It's an area i'm not really sure on.

Link to post
Share on other sites

Requiring SSL depends upon the security level of the application. For example, forums don't necessarily need HTTPS for logins. It really depends upon what data is stored, what the business model is, how you want to spend your resources, etc.

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...