I have a list of records displayed in a Kendo grid, where I can create new records in-line. The requirement is, that every record would have a unique name.
When adding a new record, I have this function, to iterate through a list of records, and check if the new records name is unique before adding it. It goes as an extension to kendo.ui.validator, adding custom rule to it. The function:
//...
uniquenamevalidation: function (input, params) {
var errorCount = 0;
//check for the rule attribute
if (input.filter("[data-val-uniquenamevalidation]").length && input.val()) {
var dataSource = $("#grid").data("kendoGrid").dataSource;
var data = dataSource.data();
//iterating through all grid elements' Names, and comparing to input
for (var i = 1; i < data.length; i++) {
if (input.val() == data[i].Name) {
errorCount++; //if there are any matches, increase error count
}
}
}
console.log(errorCount == 0);
return errorCount == 0;//returns TRUE if there are no matches, therefore input is unique
}
When the input is invalid, function returns false and validation should fail, it fails with proper error message as expected http://snag.gy/d5TZR.jpg
The problem : When the input is valid, function returns true and validation should pass, this inexplicable error appears and doesn't let me continue. http://snag.gy/syxds.jpg
So regarding to Telerik Demos I noticed I was missing a "return true" statement at the end of the function; It should look like this
uniquenamevalidation: function (input, params) {
var errorCount = 0;
//check for the rule attribute
if (input.filter("[data-val-uniquenamevalidation]").length && input.val()) {
var dataSource = $("#grid").data("kendoGrid").dataSource;
var data = dataSource.data();
for (var i = 1; i < data.length; i++) {
if (input.val() == data[i].Name) {
errorCount++;
}
}
return errorCount == 0;
}
return true;
}
Looks like for every field to be validated, all the rules are called and passed the argument of a single field, which is then filtered by the first if-statement, and the rules which dont care about this field, just return true.
Not sure how correct is this, but its working fine now, and this is the only logical explanation I could think of.