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.
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:
$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.
Total 4 comments
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?
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
It's a shame this extension has to exist! I think Yii should do this natively.
Thanks for the work anyway...
Cheers
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 login to leave your comment.