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

Grid - own validation rule

1 Answer 66 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Gusev
Top achievements
Rank 1
Gusev asked on 25 Sep 2017, 01:47 PM

Hello,

I have two grids (#Grid1 and #Grid2) with a field "Column"
I need the field "Column" in the second grid to has a value less than a value from page.
According to this topic:
http://demos.telerik.com/aspnet-mvc/grid/editing-custom-validation
I just have to write "$.extend(true, kendo.ui.validator" and "a rule", but it will affect two grids
So a question sounds like: "How to write a validation rule that will affect only one grid in page?"

1 Answer, 1 is accepted

Sort by
0
Accepted
Georgi
Telerik team
answered on 27 Sep 2017, 01:59 PM
Hello Gusev,

Possible solution is to add custom attribute to the column of the second grid and in the validation check if the input is within element with such attribute.

Add custom attribute:
columns.Bound(x => x.FirstName).HtmlAttributes(new {customAttribute = true });

Validation:
//register custom validation rules
(function ($, kendo) {
$.extend(true, kendo.ui.validator, {
rules: { // custom rules
firstnamevalidation: function (input, params) {
if (input.is("[name='FirstName']") && input.val() != "" && input.parent().is("[customAttribute]")) {
input.attr("data-firstnamevalidation-msg", "First Name should start with capital letter");
return /^[A-Z]/.test(input.val());
}
 
return true;
}
},
messages: { //custom rules messages
 firstnamevalidation: function (input) {
// return the message text
                    return input.attr("data-val-firstnamevalidation");
            }
        }
    });
})(jQuery, kendo);
 
//show server errors if any
function error_handler(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);
    }
}

For your convenience I have assembled a small sample (two-grids-one-validation.zip) which illustrates the aforementioned approach.


Regards,
Georgi
Progress Telerik
Try our brand new, jQuery-free Angular 2 components built from ground-up which deliver the business app essential building blocks - a grid component, data visualization (charts) and form elements.
Tags
Grid
Asked by
Gusev
Top achievements
Rank 1
Answers by
Georgi
Telerik team
Share this question
or