1

I am using jqgrid and using the inline editing mode and can't figure out how to return errors back to the client from server side validation rules

I use fluent validation on my serverside to validate before persisting to a database. This works great except I don't see how to return errors when editing in inline mode. If I don't persist the values to the databse, the client still shows the value which should be rejected.

What is the recommended way to return an error after someone commits an inline edit so you will get some popup on the client side showing the error and it will stay in edit state ?


NOTE: this image below is in response to Oleg's comment and answer below

enter image description here

leora
  • 188,729
  • 360
  • 878
  • 1,366

1 Answers1

2

The recommend way is to use any HTTP error code in the response on the submitting of wrong data and to return the error description in the body of the response. If you need some more specific action like displaying another dialog with the error information, setting of focus on a field, marking some fields with CSS class 'ui-state-error' or something like that you should use errorfunc callback function.

If restoreAfterError is false the inline editing will be continued.

UPDATED: I mention in comments that the server should produce the error message as the response. In case of ASP.NET MVC the default message is HTML text which you posted as the first picture. If you use HandleJsonExceptionAttribute which I described in my old answer the error message will be serialized as JSON, but it contains additional information which you don't need to display (like the StackTrace). So you should use errorfunc parameter of editRow or saveRow to decode the server response. You can either use decodeErrorMessage from the already referenced answer or use the $.parseJSON function directly:

errorfunc: function(rowid, res) {
    var errorText = $.parseJSON(res.responseText).Message;
    $.jgrid.info_dialog($.jgrid.errors.errcap,
        '<div class="ui-state-error">' + errorText + '</div>',
        $.jgrid.edit.bClose,
        {buttonalign: 'right'});
}
Community
  • 1
  • 1
Oleg
  • 220,925
  • 34
  • 403
  • 798
  • @leora: Any code grater or equal to 400 (see [here](http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4) or [here](http://en.wikipedia.org/wiki/List_of_HTTP_status_codes#4xx_Client_Error)). The error code itself will be not displayed per default in the error message displayed by jqGrid. Only the response body will bi displayed. – Oleg Jan 22 '12 at 19:53
  • @leora: If you use ASP.NET MVC for example you can just throw an exception. The `HandleJsonExceptionAttribute` from [the answer](http://stackoverflow.com/a/5501644/315935) could be used. There are many other ways to send and error message and error HTTP status code. – Oleg Jan 22 '12 at 20:34
  • this sort of works but it gives a pretty ugly popup. I attached an image of what i see after putting in an exception. Any suggestions on how to do this in a better way ? – leora Jan 22 '12 at 21:17
  • @leora: How you implemented your server part? Your current exception handler produces HTML response with the text which you posted as the image. Look at my previous comment where I gives the reference how pure JSON response can be implemented. In the way you can pure generate text response on some exceptions. Other way is the implement `errorfunc` callback function which parse the HTML which produce the server currently and display only the main part of the error message: "'Jan' must be less then 101." – Oleg Jan 22 '12 at 21:30
  • i tried this Json attribute that you mentioned but i am still seeing the whole error message (see new picture added to the question) – leora Jan 23 '12 at 02:48
  • @leora: You should parse the JSON response and display only the important part of the server response. See "UPDATED" part of my answer. – Oleg Jan 23 '12 at 07:46