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.



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


<div class="container">


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


                'Create Team',



<!-- login form-->


			<div class="form">

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






                  		  )); ?>


                     	<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 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   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'))?>


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




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




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




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

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

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


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


public function actionIndex()


			$model= new Teamname; 


			$model1= new Gametype;








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

			$model2 = new User();



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





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





And in your form

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

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


 <?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





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


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


I Think this


should be