How to display server side validation errors without multiple duplicate implementations of the error handler

1 Answer 65 Views
Grid
Stephen
Top achievements
Rank 1
Iron
Stephen asked on 04 Dec 2024, 05:19 PM

Based on step 2 in the solution in the following documentation: https://docs.telerik.com/aspnet-core/knowledge-base/grid-popup-editing-show-server-validation-errors

In order to get server side errors to display I would need to prevent databinding on the grid and then update grid elements. I would do this by first getting a reference to the grid via $("#gridName").

This would mean having a separate implemtation of this error handler for every grid in my application. Is there really no way of getting a reference to the grid or even the grid name from the error event itself?

I'd really like to avoid writing:

order_error_handler, orderline_error_handler, product_error_handler, product_category_error_handler, etc, etc. as this will be error prone and time consuming.

1 Answer, 1 is accepted

Sort by
0
Accepted
Stephen
Top achievements
Rank 1
Iron
answered on 06 Dec 2024, 10:44 AM
Answer from Anton Mironov:

Yes, you are totally correct - we can implement a generic handler for the errors raised by a request from the dataSource.

In order to achieve the pointed behavior, I decided to invest time in preparing a sample project for the case.

I am raising an error in the Update Action Method in the Controller, so for testing it on your side, please update a record in the Grid implemented and observe the result in the Dev Tools Console.

As the Error Event is part of the DataSource, we have access to it. Here is how I get the instance of the current Grid and its id(name):

    function genericErrorHandler(e) {
        var dataSource = e.sender;

        var grid = $("div[data-role='grid']").filter(function() {
            return $(this).data("kendoGrid").dataSource === dataSource;
        }).data("kendoGrid");

        console.log(grid);

        if (grid) {
            var gridName = grid.element.attr('id'); 
            console.log("Error in Grid: " + gridName);

            e.preventDefault();
        }
    }
Attached is the sample project that I prepared for the case. Feel free to test it on your side. I hope you will like the result.

 

Furthermore, the following BlogPost provides additional information about the Server-Side validation. It does not include the approach above, so I am thinking that this can be added as a new article. Please advice if it will be beneficial.


Anton Mironov
Telerik team
commented on 09 Dec 2024, 08:38 AM

Hi Stephen,

Thank you for sharing the approach with the community!

Kind Regards,
Anton Mironov
Tags
Grid
Asked by
Stephen
Top achievements
Rank 1
Iron
Answers by
Stephen
Top achievements
Rank 1
Iron
Share this question
or