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


			if(isset($_POST['UserLogin']))


			{


				$model->attributes=$_POST['UserLogin'];


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


				if($model->validate()) {


					$this->lastViset();


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


						$this->redirect(Yii::app()->controller->module->returnUrl);


					else


						$this->redirect(Yii::app()->user->returnUrl);


				}


			}


			// display the login form


			$this->render('/user/login',array('model'=>$model));


		} else


			$this->redirect(Yii::app()->controller->module->returnUrl);


	}


	


	private function lastViset() {


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


		$lastVisit->lastvisit = time();


		$lastVisit->save();


	}





}

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));

				Yii::app()->end();

			}

			

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

		    	$this->redirect(Yii::app()->controller->module->profileUrl);

		    } else {

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

					$model->attributes=$_POST['RegistrationForm'];

					$profile->attributes=((isset($_POST['Profile'])?$_POST['Profile']:array()));

					if($model->validate()&&$profile->validate())

					{

						$soucePassword = $model->password;

						$model->activkey=UserModule::encrypting(microtime().$model->password);

						$model->password=UserModule::encrypting($model->password);

						$model->verifyPassword=UserModule::encrypting($model->verifyPassword);

						$model->createtime=time();

						$model->lastvisit=((Yii::app()->controller->module->loginNotActiv||(Yii::app()->controller->module->activeAfterRegister&&Yii::app()->controller->module->sendActivationMail==false))&&Yii::app()->controller->module->autoLogin)?time():0;

						$model->superuser=0;

						$model->status=((Yii::app()->controller->module->activeAfterRegister)?User::STATUS_ACTIVE:User::STATUS_NOACTIVE);

						

						if ($model->save()) {

							$profile->user_id=$model->id;

							$profile->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);

									$identity->authenticate();

									Yii::app()->user->login($identity,0);

									$this->redirect(Yii::app()->controller->module->returnUrl);

							} 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."));

								}

								$this->refresh();

							}

						}

					} else $profile->validate();

				}

			    $this->render('/user/registration',array('model'=>$model,'profile'=>$profile));

		    }

	}

}



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…

Thanks!!!