Yii Framework Forum: How To Show Form Errorsummary On Ajax Requests - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

How To Show Form Errorsummary On Ajax Requests Rate Topic: ***** 1 Votes

#1 User is online   msoa 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 257
  • Joined: 15-August 12

Posted 13 April 2013 - 01:24 AM

When using traditional Post method form errorsummary(#some-form_es_) automatically shown form errors on submitting the form. how do it on Ajax requests?

view file:

<div class="form" >
    <?php
    $form = $this->beginWidget('CActiveForm',array(
        'id'=>'change-profile-form',
        'enableAjaxValidation'=>true,
        'enableClientValidation'=>true,
        'action' => Yii::app()->createUrl('upanel/user/CProfile'),
        'method' => 'POST',
        'clientOptions'=>array(
            'validateOnSubmit'=>true,
            'validateOnChange'=>true,
            'validateOnType'=>false,
        ),
    ));
    ?>
    .
    .
    .
    <div class="row">
        <?php
            echo CHtml::ajaxSubmitButton(
                'update',
                Yii::app()->createUrl('upanel/user/CProfile'),
                array(
                    'type'=>'POST',
                    'data'=>"js:$('#change-profile-form').serialize()",
                    'success'=>'callback',
                    'beforeSend'=>'before',
                ),
                array(
                    'id'=>'update-button'.uniqid(),
                    'class'=>'submit-button',
                )
            );
        ?>
    </div>
    <?php $this->endWidget() ?>

</div> <!-- End The Profile Form  -->
<?php echo $form->errorSummary($model,'Please solve these errors:') ?>

0

#2 User is offline   Trejder 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 1,193
  • Joined: 06-October 10
  • Location:Southern Poland

Posted 16 April 2013 - 08:54 AM

View Postmsoa, on 13 April 2013 - 01:24 AM, said:

When using traditional Post method form errorsummary(#some-form_es_) automatically shown form errors on submitting the form. how do it on Ajax requests?

A very quick reply, written by-hand, without testing.

You have:

'success'=>'callback',

in your ajaxSubmitButton options.

You have to write (and register using CClientScript) your own jQuery/Javascript function, and put it's name into that success part (or you can use anonymous function and write function code directly into success part). In this function you have to receive Ajax result, check, if it is an error, and if so, manually put error message to your error summary object (#some-form_es_).

I'm not sure, if this is somehow automated in Yii, so I think you have to do this manually, just as I wrote above. At least this is, how I would try to do this.

Test this and try to solve it yourself. If you fail, write again, I'll try to help you in more details tomorrow.
Proud Cookbook author, though still learning powerful Yii! :] See my generic profile for more information. Cheers!
0

#3 User is online   msoa 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 257
  • Joined: 15-August 12

Posted 16 April 2013 - 11:56 AM

View PostTrejder, on 16 April 2013 - 08:54 AM, said:

A very quick reply, written by-hand, without testing.

You have:

'success'=>'callback',

in your ajaxSubmitButton options.

You have to write (and register using CClientScript) your own jQuery/Javascript function, and put it's name into that success part (or you can use anonymous function and write function code directly into success part). In this function you have to receive Ajax result, check, if it is an error, and if so, manually put error message to your error summary object (#some-form_es_).

I'm not sure, if this is somehow automated in Yii, so I think you have to do this manually, just as I wrote above. At least this is, how I would try to do this.

Test this and try to solve it yourself. If you fail, write again, I'll try to help you in more details tomorrow.

Thanks dear friend, I've done it before and will share soon the own way.
0

#4 User is online   msoa 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 257
  • Joined: 15-August 12

Posted 21 April 2013 - 09:07 AM

My practice:

in controller have an method that collect model errors:

	protected function getErrorSummary($model)
	{
    	if(Yii::app()->request->isAjaxRequest) {
        	$errors=CActiveForm::validate($model);
        	if($errors !== '[]') Yii::app()->end($errors); // Sends model errors as json object
	}


in view have a CActiveForm named email-form(id), the ajaxSubmitButton is set as following:

        	echo CHtml::ajaxSubmitButton(
            	'update',
            	array(
                	'dataType'=>'json',
                	'type' => 'POST',
                	'data' => "js:$('#email-form').serialize()",
                	'success'=>'function(data){
                    	if(data.status=="success")
                    	{
                        	hideFormErrors(form="#email-form");
                        	callback(status=data.status);
                    	}else{
                        	formErrors(data,form="#email-form");
                    	}
                	}',
            	),
        	);


and at final JQuery functions:

function formErrors(data,form){
	var summary = '';
	summary="<p>Please solve following errors:</p>";

	$.each(data, function(key, val) {
    	$(form+" #"+key+"_em_").html(val.toString());
    	$(form+" #"+key+"_em_").show();

    	$("#"+key).parent().addClass("row error");
    	summary = summary + "<ul><li>" + val.toString() + "</li></ul>";
	});
	$(form+"_es_").html(summary.toString());
	$(form+"_es_").show();

	$("[id^='update-button']").show();
	$('#ajax-status').hide();//css({display:'none'});
	$('#ajax-status').text('');
}

function hideFormErrors(form){
	//alert (form+"_es_");
	$(form+"_es_").html('');
	$(form+"_es_").hide();
	$("[id$='_em_']").html('');
}

This way holds main functionality of CActiveForm::errorSummary such as css styles. with snippet manipulation you can customize the script for your functionality.

I hope be useful

2

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users