Yii 1.1: ajaxvalidationmessages

Shows validation messages coming from an AJAX-called action.
12 followers

EAjaxValidationMessages loads a jQuery plugin which show the validation error messages and changes the CSS class for the error class. It also contains a function to hide those errors and restore the form to its original state.

Changelog

  • 20110916 - 1.1 - summary is shown if it exists.

Requirements

  • Yii 1.8.0
  • jQuery

Usage

In your AJAX-called action validate like this:

$errors = CActiveForm::validate($model);
if ($errors !== '[]') {
   echo $errors;
   Yii::app()->end();
}

In your view include the widget:

<?php $this->widget('application.extensions.ajaxvalidationmessages.EAjaxValidationMessages',
                    array(
                       'errorCssClass'=>'clsError',
                       'errorMessageCssClass'=>'clsErrorMessage',
                       'errorSummaryCssClass'=>'clsErrorSummary')); ?>

In your view, in your AJAX callbacks for ajaxSubmitButtons which validate, call the plugin's methods:

'success'=>"function(html) {
    if (html.indexOf('{')==0) {
       jQuery('#the-form').ajaxvalidationmessages('show', html);
    }
    else {
       jQuery('#the-form').ajaxvalidationmessages('hide');
    }
 }",
 'error'=>"function(html) {
    jQuery('#the-form').ajaxvalidationmessages('hide');
 }",

where the-form is the id of your form.

SHA1 Checksums:

  • 74eef562a616d68302f1279befeafd3fac45007a ajaxvalidationmessages-1.0.tar.bz2

Resources

Total 6 comments

#15385 report it
egulhan at 2013/11/04 08:36am
Cool extension!

Would be better to show one error message for one field. For example, when I got these errors {"IpRelayForm_ip":["IP cant be empty.","IP is invalid."],"IpRelayForm_rules":["Rules cant be empty."]} 2 error messages are shown for the field "IpRelayForm_ip".

Thanks.

#14571 report it
Victor Porton at 2013/08/23 12:52pm
Patch for not parsing JSON twice

Get a patch. This patch is useful in the case if an AJAX action in a controller returns not only validation information, but also other information in a JSON object, so that we need to access a part of the object, not the entire JSON string.

diff '--exclude=~' '--exclude=.b[ac]k' --exclude .svn --exclude .libs --exclude .deps --exclude autom4te.cache -Naur ajaxvalidationmessages.OLD/assets/jquery/jquery.ajaxvalidationmessages.js ajaxvalidationmessages/assets/jquery/jquery.ajaxvalidationmessages.js
--- ajaxvalidationmessages.OLD/assets/jquery/jquery.ajaxvalidationmessages.js   2011-09-16 19:20:25.000000000 +0300
+++ ajaxvalidationmessages/assets/jquery/jquery.ajaxvalidationmessages.js   2013-08-23 19:48:55.503970803 +0300
@@ -38,7 +38,7 @@
          settings = $.extend({}, settings, options);
       },
       show: function(json) {
-         var e = jQuery.parseJSON(json);
+         var e = typeof json == 'object'? json : jQuery.parseJSON(json);
          var summary = '';
          jQuery.each(e, function(key, value) {
             jQuery('#'+key+'em').html(value.toString()).show();
#12816 report it
msoa at 2013/04/13 09:08am
Error hide Issue

Thanks for your effort, 1- I have several fields, when filling the fields, the last field altered the error remain on it and don't hide! 2- What's your mean from this statement: 20110916 - 1.1 - summary is shown if it exists. How to must ensure that it exist?

#8195 report it
Victor Silver at 2012/05/16 09:25am
well done

I could se the json response but would have the additional work developing a widget to handle it. Thanks to you thatś one hurdle overcame

#6122 report it
fleuryc at 2011/12/14 09:29am
too bad this extension exists...

It's a shame this extension has to exist! I think Yii should do this natively.

Thanks for the work anyway...

Cheers

#5119 report it
Mukesh at 2011/09/15 11:07am
great extension

Would be great if you could make it more customizable. Like, ability to just show error summary or apply css from some other file.

Still a great addition. Thanks

Leave a comment

Please to leave your comment.

Create extension