JqGrid forms server validations and custom error messages

左心房为你撑大大i 提交于 2019-12-28 04:34:04

问题


I have editable grid with toolbar that has buttons for editting adding and deleting records.

  1. I would like to use server side validations from my asp.mvc to display validation messages on jqgrid edit form. (is this posible?)
  2. I would like to overide message (Internal server Error ...) on edit form when exception occurs in the application. (this should be possible but I just cant figure out how to do this, perhaps using errorTextFormat, but how? )

Can someone provide example?


回答1:


You are right that errorTextFormat is the correct way to get server response in case of HTTP errors and display the corresponding error message.

First of all your server have to return response with an HTTP error code in the HTTP header. Then you should define your implementation of the errorTextFormat event handle either as a part of prmEdit, prmAdd, prmDel parameters of the navGrid or you can overwrite jqGrid default settings (see here). I personally prefer to set errorTextFormat by modifying of jQuery.jgrid.edit and jQuery.jgrid.del. An example of the corresponding code you can find in the following old answer.

The exact code of errorTextFormat function should depend on the format of the server response. I use ASP.NET MVC with WFC inside of the site and the server can return either JSON encoded string response (if the error come from throw new WebFaultException<string> ("my error text", statusCode); which I thrown explicitly) or sometime HTML response. In my implementation of errorTextFormat I test which kind of error response I received and convert the server response. Here is the code fragment:

my.errorTextFormat = function (data) {
    var str = data.responseText.substr(0, 1);
    var str1 = data.responseText.substr(0, 6).toLowerCase();
    if (str === '"') {
        var errorDetail = jQuery.parseJSON(data.responseText);
        var s = "Fehler: '";
        s += data.statusText;
        s += "'. Details: ";
        s += errorDetail;
        return s;
    }
    else if (str1 === "<html " || str1 == "<html>" ||
             data.responseText.substr(0, 169) === '<?xml version="1.0" encoding="utf-8"?>\r\n<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">\r\n<html ') {
        var bodyHtml = /<body.*?>([\s\S]*)<\/body>/.exec(data.responseText)[1];
        return bodyHtml; //bodyContents1;
    }
    else {
        var res = "Status: '";
        res += data.statusText;
        res += "'. Felhercode: ";
        res += data.status;
        return res;
    }
};
jQuery.extend(jQuery.jgrid.edit, {
    ...
    errorTextFormat: my.errorTextFormat
});
jQuery.extend(jQuery.jgrid.del, {
    ...
    errorTextFormat: Testportal.errorTextFormat
});

The code is not perfect, but you can use it to create your own one.



来源:https://stackoverflow.com/questions/5103424/jqgrid-forms-server-validations-and-custom-error-messages

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!