This is a migrated thread and some comments may be shown as answers.

Grid with DataAnnotations/Validation: How to display field name in the JS error message?

2 Answers 126 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Mike
Top achievements
Rank 1
Mike asked on 01 Sep 2012, 08:23 PM
Hello,

I have an MVC application that uses the Grid control.  I have configured .NET DataAnnotations, which includes error messages, and it is working.  When there is a data entry validation error, Javascript displays my error message(s).  The issue that I have is that each message that is displayed does not tell me the name of the field that has invalid data.  The following is the Javascript function that Kendo uses in its examples.  I can't find details as how to add the field name to the message that is displayed.  I hope that someone can help me.

Thanks,

Mike

function error(e) {
    if (e.errors) {
        var message = "Errors:\n";
        $.each(e.errors, function (key, value) {
            if ('errors' in value) {
                $.each(value.errors, function () {                   
                    message += this + "\n";
                });
            }
        });

        alert(message);
    }
}
</script>



2 Answers, 1 is accepted

Sort by
0
Accepted
Daniel
Telerik team
answered on 05 Sep 2012, 03:43 PM
Hello Mike,

The key of the error holds the name of the field. You could use it to generate a message which includes the property names for the errors e.g.

$.each(e.errors, function (key, value) {
    if ('errors' in value) {
        message += key + ":\n";
        $.each(value.errors, function () {
            message += this + "\n";
        });
    }
});
Regards,
Daniel
the Telerik team
Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
0
Mike
Top achievements
Rank 1
answered on 05 Sep 2012, 05:58 PM
Thank you, Daniel!
Tags
Grid
Asked by
Mike
Top achievements
Rank 1
Answers by
Daniel
Telerik team
Mike
Top achievements
Rank 1
Share this question
or