Jonathon Posted March 25, 2013 Share Posted March 25, 2013 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 comment Share on other sites More sharing options...
Larry Posted March 26, 2013 Share Posted March 26, 2013 That looks pretty good. Could you confirm what's reported as undefined? Are you saying that code (the second bit) is in a view file or a layout file? (i.e., specifically what file is it in?) Link to comment Share on other sites More sharing options...
Jonathon Posted March 26, 2013 Author Share Posted March 26, 2013 Oh sorry, The layout file is where the code (The 2nd part) for the form resides.In my case layouts/home.php and the $loginModel is returned as undefined when I access the index page. Link to comment Share on other sites More sharing options...
Larry Posted March 27, 2013 Share Posted March 27, 2013 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. 1 Link to comment Share on other sites More sharing options...
Jonathon Posted March 27, 2013 Author Share Posted March 27, 2013 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 comment Share on other sites More sharing options...
Jonathon Posted March 27, 2013 Author Share Posted March 27, 2013 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 comment Share on other sites More sharing options...
Larry Posted March 27, 2013 Share Posted March 27, 2013 Thanks for sharing that, Jonathon. As for the layouts issue, take a look at the Yii template site with the column1 and column2 files that allow for one main layout file with two subversions. Link to comment Share on other sites More sharing options...
Jonathon Posted March 28, 2013 Author Share Posted March 28, 2013 Thanks Larry, will do. Link to comment Share on other sites More sharing options...
Jonathon Posted March 30, 2013 Author Share Posted March 30, 2013 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 comment Share on other sites More sharing options...
Larry Posted April 1, 2013 Share Posted April 1, 2013 I *think* you should be able to assign $loginModel a value of new LoginForm in a constructor in Controller.php. Untested, but that's where I would start. Link to comment Share on other sites More sharing options...
Jonathon Posted April 1, 2013 Author Share Posted April 1, 2013 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 comment Share on other sites More sharing options...
Larry Posted April 1, 2013 Share Posted April 1, 2013 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 comment Share on other sites More sharing options...
Jonathon Posted April 2, 2013 Author Share Posted April 2, 2013 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 comment Share on other sites More sharing options...
Larry Posted April 2, 2013 Share Posted April 2, 2013 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 comment Share on other sites More sharing options...
Jonathon Posted April 2, 2013 Author Share Posted April 2, 2013 Hi Larry, Thank you, I need to look into it more. I do like my drop down log in box. But i'm not sure that I really need full site wide SSL.So maybe I won't use it for Logins. Link to comment Share on other sites More sharing options...
Recommended Posts