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 ¶
- Original thread <- problem to solve discussed here.
- Extension thread <- please comment, report bugs, send kudos or send flames here.
User Contributed Notes 6
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
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
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
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?
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();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.
Leave a comment
If you have any questions, please ask in the forum instead.
Join the conversation to share a note.