Display Validation Errors in CJuiDialog

I have a form displayed in a CJuiDialog and when form is submitted I want the controller action to dynamically update the dialog if there are any validation errors. I assume I need to use an ajaxSubmit button in the form. Could someone provide an example of the ajax button configuration required in the form and the necessary changes required in the controller action to handle displaying the form normally and then updating the form when validation errors are detected?

Following Group form fragment is displayed in a CJuiDialog




<?php $form=$this->beginWidget('CActiveForm', array('id'=>'group-form', 'enableAjaxValidation'=>true)); ?>

   ....


   <?php echo CHtml::ajaxSubmitButton('Save',$this->createUrl('group/create'), ... ); ?>


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

 

Group Controller action




public function actionCreate()

	{

		$model=new Group;

		$this->performAjaxValidation($model);


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

		{

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

		       if($model->save())

				$this->redirect(array('view','id'=>$model->id));

                       else

                           // validation error so just update the existing form inside the CJuiDialog

		}


                // create displays CJuiDialog

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

	}



Anyone have any info on how to do this??? :unsure:

Did you end up finding any help for this? im in the same situation

I’ve done this recently. It was yii-boostrap, so I didn’t use CJuiDialog, but I don’t thing it changes much.

The principle is that the AJAX action return a JSON answer that is parsed as an object by jQuery.

Here is what I put in the controller:




if ($model->save()) {

	if (Yii::app()->getRequest()->isAjaxRequest) {

		$this->header('json'); // custom controller method that sends the right header()

		echo json_encode(array(

			'id' => $model->id,

			'result' => 'success',

			'label' => CHtml::encode($model->getFullName()),

			'message' => "The object <em>" . CHtml::encode($model->getFullName()) . "</em> was created."

		));

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

	} else {

		$this->redirect(array('view', 'id' => $model->id));

	}

} else if (Yii::app()->getRequest()->isAjaxRequest) {

	$this->header('json');

		echo json_encode(array(

			'result' => 'error',

			'html' => $this->renderPartial(

				'_form', array('model' => $model, 'noButton' => true), true

			)

		));

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

}



The JS included in the view:




$('#editor-form').submit(function() {

	$.ajax({

		type: 'POST',

		url: $(this).attr("action"),

		data: $(this).serialize(),

		success: function (data) {

			if (data.result == 'success') {

				$('#modal-editor-create').modal('hide');

				addNewEditor(data.id, data.label);

				$("#modal-editor-create-link").closest('.controls').append(

					'<div class="alert alert-success">' + data.message + '</div>'

				);

			} else {

				$('#modal-editor-create .modal-body').html(data.html);

			}

		},

		dataType: 'json'

	});

	return false; // prevent normal submit

});

// append the result of the dialog to the main (non-AJAX) form

function addNewEditor(id, label) {

	jQuery('#EditorNew').clone()

		.find('label').append(label).end()

		.find('input').val(id).attr('checked', 'checked').end()

		.insertBefore('#EditorComplete').removeClass('hidden');

}