Ajax Validation -2 models, one form

Hi All,

I am facing this problem where Ajax validation is activating. I want to validate the form before user submits it.

Believe me, I have gone through all the already posted forum questions and applied suggestions from them. But I could not make it work. Please help.

Thanks,

Vic

Whats happening (or not)

  • No Ajax validation when I tab out of teamname text field

  • No Ajax validation when do not select an option from dropdown

  • On form submit, my page refreshes and I lose all the info.

(Note: This code creates a team. Whenever user enters atleast one player. Then a "create team" submit button is added dynamically. This ajax action is for that "create team"submit button)

Here is my code

View




<div class="container">

			<?php

            $this->pageTitle=Yii::app()->name . 'Create Team';

            $this->breadcrumbs=array(

                'Create Team',

            );

            ?>




<!-- login form-->


           




			<div class="form">

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

                         'id'=>'createteam-form',

                        'enableAjaxValidation'=>true,

						 'clientOptions'=>array('validateOnSubmit'=>true),

						 

                                        

                  		  )); ?>

            		

                     	<div align="center" class="row" >  

                               

                     <p align="center">Fields with <span class="required">*</span> are required.</p>

                               

                   <?php echo $form->errorSummary($model); ?>

                   <?php echo $form->errorSummary($model1); ?>

                

               					  

                     <h2> <?php echo $form->labelEx($model,"team name",array('class'=>'blue round label')); ?></h2>

                    <?php echo $form->textField($model,'teamname',array('id'=>'teamname', 'size'=>60,'maxlength'=>200,'class'=>'medium input-text')); ?>

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

                                              <hr><!--Team name text Field End-->

                               

											 <?php $list = CHtml::listData($model1,'gametypeid','gametype');?>

                                             <?php echo CHtml::dropDownList('Gametype',$select, $list,array('empty' => 'Select a game'));?>

                      <hr> <!--Game select End-->

                                    

                     <h1><?php echo CHtml::activeLabel($model2, "Add Players",array('class'=>'blue round label'))?></h1><br>

                                  

                       <div style=" display:inline-block;" > 

                                  

                       <h2> <?php echo CHtml::activeLabel($model2, "fname",array('class'=>'green round label'))?>	</h2>

                       <?php echo CHtml::activeTextField($model2, "fname",array('id'=>'fname','name'=>'fname','class'=>'small input-text'))?>

                                             

                      </div> 

                                         

                                           

                       <div style=" display:inline-block;" > 

                        <h2><?php echo CHtml::activeLabel($model2, "lname",array('class'=>'green round label'))?></h2>

                        <?php echo CHtml::activeTextField($model2, "lname",array('id'=>'lname','name'=>'lname','class'=>'small input-text'))?>

                                              </div>

                                              

                          <br><br>

										

                         <div   style="display:inline-block;" >

											  <h2> <?php echo CHtml::activeLabel($model2, "email",array('class'=>'green round label'))?></h2>

                                    

											  <?php echo CHtml::activeTextField($model2, "email",array('id'=>'email','name'=>'email','class'=>'medium input-text'))?>

                                               <br>

                      <?php echo CHtml::button('+',array('id'=>'player-add', 'class'=>'black round button'))?>

                                    		 </div>

                                

                       <fieldset>   

                        <div id="createTeam" align="center" style="font-color:#FFF">

                         </br>

                         </div>

                                         

                        <ul id="newFields" align="center" style="font-color:#GFF">

                         </ul>

                       </fieldset>  

                                            

                          </div>  <!--ROW END-->

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

 					</div><!--FORM END-->

                                            <br>


</div><!--container END-->



CONTROLLER


public function actionIndex()

{

			$model= new Teamname; 

			

			$model1= new Gametype;

		

	

			}

         $this->performAjaxValidation($model);

       $this->performAjaxValidation($model1);

			

			

        	$model1 = Gametype::model()->findAll();

			$model2 = new User();

			$fname='';

			$lname='';

			$select = array(); 

			

		    

			 

			$this->render('createteam', array('model'=>$model,'model1'=>$model1, 'model2'=>$model2,  'select'=>$select,'fname'=>$fname,'lname'=>$lname));

	

	

	}

	protected function performAjaxValidation($model)

	{

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

		{

			echo CActiveForm::validate($model);

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

		}

	}



[b]

Model- Teamname[/b]


  public function rules()

    {

        // NOTE: you should only define rules for those attributes that

        // will receive user inputs.

        return array(

            array('teamname', 'required'),

			array('teamname', 'unique'),

			array('teamname', 'length', 'max'=>45),

			

            // The following rule is used by search().

            // Please remove those attributes that should not be searched.

            array('teamnameid, teamname', 'safe', 'on'=>'search'),

        );

    }



Hi Vic,

In your controller add like this




$this->performAjaxValidation($model);

$this->performAjaxValidation($model1);



to




$this->performAjaxValidation($model,$model1);



And in your form




 <?php echo $form->errorSummary($model); ?>

 <?php echo $form->errorSummary($model1); ?>



to




 <?php echo $form->errorSummary($model,$model1); ?>



If you wish to not perform ajax validation when you tab out of teamname then check the value is null or not then perform the action.

If you do not want to lose the data entered by the user then set like this in your controller


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

{

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

   $model1->attributes=$_POST['Gametype'];

    .......rest of the code to save the data.....

}

call the models before the post validation so that you wont lose any data.

Thanks

I Think this




$this->performAjaxValidation($model,$model1);



should be




$this->performAjaxValidation(array($model,$model1));