2 forms in one view

Hi all,

I wanted to have sign up and log in on one page (2 tabs, 2 different submit buttons). I’m not sure how to determine which form was submitted and to fetch the corresponding data in the controller. I thought I might be able to use the id of the form but it didn’t work. How would you go about it?

Thanks…




// Controller:


public function actionLogin()

	{

		/* log in */

		

		$model=new LoginForm;


		// if it is ajax validation request

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

		{

			echo CActiveForm::validate($model);

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

		}


		// collect user input data

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

		{

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

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

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

			{										

				

					

				

				$this->redirect(Yii::app()->homeUrl);

								

				

			}

				

		}

		

		

		/* sign up */

				

		$teacher = new Teacher;			

		

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

		{		 			

			

			$teacher->attributes=$_POST['SignupForm'];

;	

		

			if ($teacher->save()) {

				 				  				 


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


				

				

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

				{

																		

					$this->redirect(Yii::app()->homeUrl);

					

				} 

			

			} 

			

			

		}


		 

		

		// display the login form

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

	}


// view:

<!-- content of tabs -->                    

                <ul class="nice tabs-content contained">

					

                    <!-- first tab -->

                    <li<?php if ( !(isset($teacher-> email)) ) { echo " class='active'"; } /* open sign up tab when being redirected due to failed validation in sign up tab */ ?> id="nice1Tab">                           

                

						<div class="form">                												

						<?php $form=$this->beginWidget('CActiveForm', array(

                            'id'=>'login-form',

                            'enableClientValidation'=>false,

                            'clientOptions'=>array(

                                'validateOnSubmit'=>false,

                            ),

                            // foundation

                            'htmlOptions'=>array('class'=>'nice custom')

                        )); ?>

                                        

                            <div class="row" style="margin-bottom: 10px">

                                <?php echo $form->labelEx($model,'username'); ?>

                                <?php echo $form->textField($model,'username',array('class'=>'oversize input-text' /* foundation */)); ?>

                                <?php echo $form->error($model,'username'); ?>

                            </div>

                        

                            <div class="row" style="margin-bottom: 10px">

                                <?php echo $form->labelEx($model,'password'); ?>

                                <?php echo $form->passwordField($model,'password',array('class'=>'oversize input-text' /* foundation */)); ?>

                                <?php echo $form->error($model,'password'); ?>                        

                            </div>

                        

                            <div class="row rememberMe" style="margin-bottom: 15px">

                                <?php echo $form->checkBox($model,'rememberMe'); ?>

                                <?php echo $form->label($model,'rememberMe',array('style'=>'display:inline;margin-left:10px')); ?>

                                <?php echo $form->error($model,'rememberMe'); ?>

                            </div>

                        

                            <div class="row buttons">

                                <?php echo CHtml::submitButton('Login',array('class'=>'nice large radius blue button')); ?>

                            </div>

                        

                        <?php $this->endWidget(); ?>

                        </div><!-- form -->

                        					

                    </li>

                    <!-- end first tab -->

                

                    <!-- second tab -->                

					<li<?php if ( isset( $teacher-> email) ) { echo " class='active' "; } /* open sign up tab when being redirected due to failed validation in sign up tab */ ?> id="nice2Tab">

				

						<div class="form">				

						<?php $form=$this->beginWidget('CActiveForm', array(

                            'id'=>'signup-form',

                            'enableClientValidation'=>false,

                            'clientOptions'=>array(

                                'validateOnSubmit'=>false,

                            ),

                            // foundation

                            'htmlOptions'=>array('class'=>'nice custom')

                        )); ?>

                                        

                            <div class="row" style="margin-bottom: 10px">

                                <?php echo $form->labelEx($teacher,'email'); ?>

                                <?php echo $form->textField($teacher,'email',array('class'=>'oversize input-text','id'=>'user_id_sign_up' /* foundation and id for password strength indicator (incl checking whether pw = user name) */)); ?>

                                <?php echo $form->error($teacher,'email'); ?>

                            </div>

                        

                            <div class="row" style="margin-bottom: 10px">

                                <?php echo $form->labelEx($teacher,'password'); ?>

                                <?php echo $form->passwordField($teacher,'password',array('class'=>'oversize input-text','id'=>'password_sign_up' /* foundation and id for password strength indicator (incl checking whether pw = user name)*/)); ?><?php Yii::app()->clientScript->registerScript('passwordStrengthMeter',"    

								$('#password_sign_up').passStrength({

									shortPass: 		'top_shortPass',	//optional

									badPass:		'top_badPass',		//optional

									goodPass:		'top_goodPass',		//optional

									strongPass:		'top_strongPass',	//optional

									baseStyle:		'top_testresult',	//optional

									userid:			'user_id_sign_up',		//required override

									messageloc:		0			//before == 0 or after == 1					

								});")?>

                                <?php echo $form->error($teacher,'password'); ?>                        

                            </div>                                                     

                                          

                            <div class="row buttons">

                                <?php echo CHtml::submitButton('Sign Up For Free',array('class'=>'nice large radius blue button')); ?>

                            </div>

                        

                        <?php $this->endWidget(); ?>

                        </div>

                        <!-- form -->                  

                                

                    </li>

                    <!-- end second tab -->

            

            </ul>  

            <!-- end content of tabs -->    



Hi,

  1. it’s better to use different actions to separate login and signup

  2. If you feel that’s mixing login and signup is right way for you now, then you could check submit button name into post request:




...

<?php echo CHtml::submitButton('Login',array('name'=>'login', 'class'=>'nice large radius blue button')); ?>

...

if (isset($_POST['login'])) { ... login form was submitted ... }

...

<?php echo CHtml::submitButton('Sign Up For Free',array('name'=>'signup', 'class'=>'nice large radius blue button')); ?>

...

if (isset($_POST['signup'])) { ... login form was submitted ... }




also, if you have 2 forms, then just one of them will come in request. So I guess checking isset($_POST[‘LoginForm’]) is enough to be sure that LoginForm was submitted.