Login and Register forms in the same page

Hello everyone,

I have a site which has a Register form and login form, in separate models, controllers and views. I’ve seen in many sites there is a tendency to put them together in the same page, so it’s more usable to the user -you may check it in eDreams.com, for instance-. The thing is that there are to submit buttons in the same page. I’ve tried several things, creating two forms, one form with two scenarios. But when clicking on Login submit button or register submit button to validate and redirect, I have a kind of erratic behaivour -or rather I don’t understand-. The POST doesn’t contain “what it should”. I’ve search in the forum and there are similar posts, but my problem persists. Please, how yould you do this? Should I have to submit the forms with Javascript buttons? I don’t put any code because I have been trying this for days, so I wouldn’t know what to put…

Thank you

I’m not sure what you mean by javascript buttons - but if you’re talking AJAX, forget that for the moment. The one rule of ajax is to get your site working without it first, and then slowly add it in once everything else is working.

First, you need to decide which controller you’re going to be using to show this login/registration page - I would recommend the controller that owns the registration action.

So how would you write the controller for this? Well,

public function actionRegister() {


	$loginFormModel = new LoginForm;

	$newUserModel = new User;


	if(isset($_POST['LoginForm'])) {

		// Process your site login here

		/* ..... */

		if($loginFormModel->validate()) {

			// save


	} else if(isset($_POST['User'])) {

		// Process your user sign-up here



	$this->render('register_login.php', array('loginFormModel'=>$loginFormModel, 'newUserModel'=>$newUserModel));


There really isn’t much more to it than that. For your register_login view:

/* register_login.php */

$this->renderPartial('application.views.user.register', array('newUserModel'=>$newUserModel));

$this->renderPartial('application.views.site.login', array('loginFormModel'=>$loginFormModel));

This will render views that belong to a different controller. As long as your set the "action" correctly (to the register action of the controller that owns register_login), then any form errors will be caught correctly and displayed in their own partial.

This is not a complete solution, but I hope it points you in the right direction.

Yes, it worked perfect. I had it working already with Javascript Submit (that’s what I meant with Javascript buttons, just the submit). But I left that path and started this way…

Thank you!

Sorry about the trouble I caused but your solution seems not work for me….

I don’t know what should I do…Would you please help me out… Thanks so much for your help and consideration……

Here is the code in my LoginController….

public function actionLogin()


		if (Yii::app()->user->isGuest) {

			$model=new UserLogin;

			// collect user input data




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

				if($model->validate()) {


					if (strpos(Yii::app()->user->returnUrl,'/index.php')!==false)






			// display the login form


		} else




	private function lastViset() {

		$lastVisit = User::model()->notsafe()->findByPk(Yii::app()->user->id);

		$lastVisit->lastvisit = time();




Here is my very long registration code….

public function actionRegistration() {

            $model = new RegistrationForm;

            $profile=new Profile;

            $profile->regMode = true;


			// ajax validator

			if(isset($_POST['ajax']) && $_POST['ajax']==='registration-form')


				echo UActiveForm::validate(array($model,$profile));




		    if (Yii::app()->user->id) {


		    } else {

		    	if(isset($_POST['RegistrationForm'])) {





						$soucePassword = $model->password;









						if ($model->save()) {



							if (Yii::app()->controller->module->sendActivationMail) {

								$activation_url = $this->createAbsoluteUrl('/user/activation/activation',array("activkey" => $model->activkey, "email" => $model->email));

								UserModule::sendMail($model->email,UserModule::t("You registered from {site_name}",array('{site_name}'=>Yii::app()->name)),UserModule::t("Please activate you account go to {activation_url}",array('{activation_url}'=>$activation_url)));



							if ((Yii::app()->controller->module->loginNotActiv||(Yii::app()->controller->module->activeAfterRegister&&Yii::app()->controller->module->sendActivationMail==false))&&Yii::app()->controller->module->autoLogin) {

									$identity=new UserIdentity($model->username,$soucePassword);




							} else {

								if (!Yii::app()->controller->module->activeAfterRegister&&!Yii::app()->controller->module->sendActivationMail) {

									Yii::app()->user->setFlash('registration',UserModule::t("Thank you for your registration. Contact Admin to activate your account."));

								} elseif(Yii::app()->controller->module->activeAfterRegister&&Yii::app()->controller->module->sendActivationMail==false) {

									Yii::app()->user->setFlash('registration',UserModule::t("Thank you for your registration. Please {{login}}.",array('{{login}}'=>CHtml::link(UserModule::t('Login'),Yii::app()->controller->module->loginUrl))));

								} elseif(Yii::app()->controller->module->loginNotActiv) {

									Yii::app()->user->setFlash('registration',UserModule::t("Thank you for your registration. Please check your email or login."));

								} else {

									Yii::app()->user->setFlash('registration',UserModule::t("Thank you for your registration. Please check your email."));





					} else $profile->validate();






I really don’t know how to revise them correctly… I did that in your way but it gives me a blank page ……. And where exactly should I put the code in my view ?

I try to put them in the bottom and it not works either…

I know it’s kind of complex but thanks soooo much if you can help me…