Why This Sometimes Load The True Url?

At the first load of my app, there will be a gridview like this

4536

4.png

then, when user click add, it will renderpartial a form using ajax like this

4533

1.png

there is validation, so user cannot create until the form is correct validated

some error messsage will appear

4534

2.png

but sometimes when user click create without input, it goes to the true url, leave my grid page

4535

3.png

what should i add please to my code to prevent this thing?

here’s my controller code




public function actionFormAdd($idhotel)

	{

		$this->layout = '//layout/ajax';

		$model=new Promo;

		$this->performAjaxValidation($model);

		

		// Uncomment the following line if AJAX validation is needed

		// $this->performAjaxValidation($model);

	

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

		{

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

			if($model->save())

				$this->redirect(array('hotel/view/id/'.$model->hotelid.'/#promocodes'));

		}


		$this->render('_tab_ajax_add',array(

			'model'=>$model,

			'idhotel'=>$idhotel,

		), false, true);

	}

I’ve tried to add this but no result


$cs = Yii::app()->clientScript;

		$cs->reset();

		$cs->scriptMap = array(

			 'jquery.js'  =>  false,   // prevent produce jquery.js in additional javascript data

		);

		Yii::app()->clientScript->registerCoreScript('jquery');

_tab_ajax_add

<div>

<?php $this->renderPartial(’_add_form’,array(

'model'=&gt;&#036;model,


'hotelid'=&gt;&#036;idhotel,

)); ?>

</div>

in _add_form I define


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

	'id'=>'promo-form',

	'enableAjaxValidation'=>true,

	'enableClientValidation'=>true,

	'clientOptions' => array(

                            'validateOnSubmit' => true),

)); 


?>

...

<?php echo CHtml::submitButton($model->isNewRecord ? 'Create' : 'Save', array("class"=>"small button"));?>



Help please…

This probably should help, There You have more then 1 solution for Your problem:

http://www.yiiframework.com/forum/index.php/topic/37075-form-validation-with-ajaxsubmitbutton/

there are only two things to be noted as follows:

  1. @ your controller call validate of your model and return a json object if form is invalid

  2. @ your view break this json object (OR traverse through it) and show the error messages under respective elements of the form.

Please see:

http://www.yiiframework.com/forum/index.php/topic/37075-form-validation-with-ajaxsubmitbutton/