Jquery Code For Yiiactiveform.js Is Not Included

Hello everybody!

I have two forms which should save data to database. If the data was successfully changed this information should be displayed to the user. So I decided to do this via ajax, because form also should still displayed to user. As the client side validation is not working with CHtml::ajaxButton or CHtml::ajaxSubmitbutton I use a CHtml::submitButton and CActiveForm afterValidate function like this:




<?php

	$postUrl = $this->createAbsoluteUrl('loginuser/saveData',array('id'=>$model->id));

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

	'id'=>'account-form',

	'enableClientValidation'=>true,

	'clientOptions'=>array(

		'validateOnSubmit'=>true,

		'afterValidate'=> "js: function afterValidate(form, data, hasError){

			if (!hasError) {

				$.ajax({

					url: '$postUrl',

					type: 'POST',

					data: $('#account-form').serialize()

				})

				.success(function ( data ) {

					dump('success',data);

					//TODO remove quote sign

					$('#label-info').text(data);

				})

				.fail(function ( xhr, status ) {

					$('#label-info').text(data);

					dump('fail',xhr.status,xhr,status);

				})

			;

			return false;

			}

		}"

	),

));

?>



my submit button:




		<?php 

			print CHtml::submitButton('Save',

					array('class'=> 'button-small green-gradient green-gradient-hover',

					      'id' => 'save-btn')

			);

  	        ?>



And the action which renders the view:




	public function actionIndex()	{

		$model = LoginUsers::model()->findByPk(Yii::app()->user->id);

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

	}



For this form all works fine, the action "saveData" will be called with correct id and I can save data to database. In firebug I can see the generated jQuery code which looks like:




<script type="text/javascript">

/*<![CDATA[*/

jQuery(function($) {

jQuery('#account-form').yiiactiveform({'validateOnSubmit':true,'afterValidate': function afterValidate(form, data, hasError){

if (!hasError) {

$.ajax({

url: 'http://192.168.75.3/xkeymanager/tags/1.0/index.php?r=loginuser/saveData&id=2',

type: 'POST',

data: $('#account-form').serialize()

})

.success(function ( data ) {

$('#label-info').text(data);

})

.fail(function ( xhr, status ) {

$('#label-info').text(data);

})

;

return false;

}

},'attributes':[{'id':'LoginUsers_firstName','inputID':'firstname','errorID':'firstname-error','model':'LoginUsers','name':'firstName','enableAjaxValidation':false,'status':1,'clientValidation':function(value, messages, attribute) {

if(jQuery.trim(value)=='') { 


....



Now I have another form with different data and I want to handle it exactly the same, so I wrote my form code:




<?php

	$postUrl = $this->createAbsoluteUrl('dealer/saveData',array('id'=>$model->id));

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

	'id'=>'dealer-form',

	'enableClientValidation'=>true,

	'clientOptions'=>array(

		'validateOnSubmit'=>true,

		'afterValidate'=> "js: function afterValidate(form, data, hasError){

			if (!hasError) {

				$.ajax({

					url: '$postUrl',

					type: 'POST',

					data: $('#dealer-form').serialize()

				})

				.success(function ( data ) {

					$('#label-info').text(data);

				})

				.fail(function ( xhr, status ) {

					$('#label-info').text(data);

				})

			;

			return false;

			}

		}"

	),

));

?>



my submit button




		<?php 

			print CHtml::submitButton('Save',

					array('class'=> 'button-small green-gradient green-gradient-hover',

					      'id' => 'save-btn')

			);

  	        ?>



and my action code




	public function actionIndex()	{

		$model = Dealers::model()->findByPk(1);

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

        }



but in this case the action function “saveDealer” would not be called. After looking in firebug I can’t find jQuery code for yiiactiveform and also no code for validation:




<script type="text/javascript">

/*<![CDATA[*/

jQuery(function($) {

jQuery('body').on('click','#account-btn',function(){jQuery.yii.submitForm(this,'/xkeymanager/tags/1.0/index.php?r=loginuser/index',{});return false;});

jQuery('body').on('click','#logout-button',function(){jQuery.yii.submitForm(this,'/xkeymanager/tags/1.0/index.php?r=login/logout',{});return false;});

});

/*]]>*/

</script> 



Any ideas what is going wrong?

Regards

Do you have any




<?php echo $form->error($model, "some_attribute"); ?>



in your form?

CActiveForm will not perform ajax validation or client validation on the field that has no error reporting. And if there’s no single field that has error reporting, then CActiveForm will not include yiiActiveForm.js.

It’s one of the ditches I often fall into. :D

Hi softark,

Thank you very much that solved my problem!

Sorry for late answer, but I didn’t have access to my code at weekend.

How can I set this topic to solved?

Regards :)

Thank you for the feedback. :)

There’s no need to close the topic, nor mark it as solved. Someone else may want to continue the discussion.