I have a CActiveForm and an ajaxSubmitButton which posts to an action. In that action I do this:
$errors = CActiveForm::validate($model);
if (!empty($errors)) {
echo $errors;
Yii::app()->end();
}
getting a JSON string in case of a validation error. How can I show those errors from the ajaxSubmitButton clientOptions.sucess callback javascript function?
doesn’t ajaxOptions.success callback function have a parameter (data or response, you name it)? That parameter is your JSON response from the server. Which basically looks like this:
Yes, that could work, but I was wondering if there’s a built-in function in Yii’s javascript code to apply those error messages to their respective CActiveForm.error labels…
'success'=>"function(html) {
if (html.indexOf('{')==0) {
var e = jQuery.parseJSON(html);
jQuery.each(e, function(key, value) {
jQuery('#'+key+'_em_').show().html(value.toString());
jQuery('#'+key).addClass('clsError');
jQuery('label[for='+key+']').addClass('clsError');
});
}
This works, but as you can see, it’s a pain in the a$$ to include this in every form you need. Is there any easier way to accomplish this? If not, it would be helpful if Yii could include a simple method to avoid including all that code. It could be a jQuery plugin, for instance, or a method inside Yii’s jquery.activeform.js code.
I agree, it would be helpful if there was some js api to yiiactiveform.
But you can also write that function in js file and include its name always when you want to use it as a callback. It would look like there “is” some method to do what you want