Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
520 views
in Technique[技术] by (71.8m points)

jquery - JqGrid forms server validations and custom error messages

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html ') {
        var bodyHtml = /<body.*?>([sS]*)</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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...