The following code is dataSource for validating values via a controller method:
SampleDataSource: new kendo.data.DataSource({
transport: {
read: {
url: "Sample/ValidateSampleCodes",
contentType: "application/json; charset=utf-8",
dataType: "json",
type: "POST"
},
parameterMap: function (options, operation) {
return JSON.stringify({ SampleCode: options.SampleCode });
},
},
schema: {
model: {
id: "Id"
},
parse: function (data) {
viewModel.set("SampleCodesFromDB",data[0]);
return data;
}
}
}),
The following code is the grid in which i need to validate values entered, instantly and display an in-line validation message. I am using the custom validator as shown below...
$("#Sample-code-grid").kendoGrid({
dataSource: {
data: viewModel.SampleCodes,
schema: {
model: {
id:"Id",
fields : {
SampleCode : { editable: true, validation: { custom: function (input) {
var isValid = true;
if (input.attr("name") == "SampleCode") {
// Reading the below datasource to check if value already exists in DB
viewModel.SampleDataSource.read({ SampleCode: input.val() });
// When debugging, i find that the above read method call is skipped until
// custom validator function is fully executed...
var isValidMessage = viewModel.get("SampleCodesFromDB");
if(isValidMessage == "Code Exists")
isValid = false;
if(!isValid)
input.attr("data-custom-msg", "Sample Code already added.");
}
return isValid;
}
}
}
}
}
},
change: function(e) {
if(e.action == "itemchange" || "remove") {debugger;
viewModel.SampleCodes = [];
$.each(e.sender.data(), function(inx, val) {
viewModel.SampleCodes.push({ SampleCode : val.SampleCode });
});
}
}
},
selectable: true,
editable: "inline",
toolbar: [{ name: "create" , text : "Add Sample Code" }],
columns: [
{ field: "SampleCode", title: "Sample Codes" },
{ command: ["edit", "destroy"], title: " ", width: "210px" }
]
});
I have added some comments inside the code so that you get some idea about my problem. I just need to know how to approach validation for duplicates using the custom validator that calls a dataSource for the same purpose.
Any help will be greatly appreciated.
SampleDataSource: new kendo.data.DataSource({
transport: {
read: {
url: "Sample/ValidateSampleCodes",
contentType: "application/json; charset=utf-8",
dataType: "json",
type: "POST"
},
parameterMap: function (options, operation) {
return JSON.stringify({ SampleCode: options.SampleCode });
},
},
schema: {
model: {
id: "Id"
},
parse: function (data) {
viewModel.set("SampleCodesFromDB",data[0]);
return data;
}
}
}),
The following code is the grid in which i need to validate values entered, instantly and display an in-line validation message. I am using the custom validator as shown below...
$("#Sample-code-grid").kendoGrid({
dataSource: {
data: viewModel.SampleCodes,
schema: {
model: {
id:"Id",
fields : {
SampleCode : { editable: true, validation: { custom: function (input) {
var isValid = true;
if (input.attr("name") == "SampleCode") {
// Reading the below datasource to check if value already exists in DB
viewModel.SampleDataSource.read({ SampleCode: input.val() });
// When debugging, i find that the above read method call is skipped until
// custom validator function is fully executed...
var isValidMessage = viewModel.get("SampleCodesFromDB");
if(isValidMessage == "Code Exists")
isValid = false;
if(!isValid)
input.attr("data-custom-msg", "Sample Code already added.");
}
return isValid;
}
}
}
}
}
},
change: function(e) {
if(e.action == "itemchange" || "remove") {debugger;
viewModel.SampleCodes = [];
$.each(e.sender.data(), function(inx, val) {
viewModel.SampleCodes.push({ SampleCode : val.SampleCode });
});
}
}
},
selectable: true,
editable: "inline",
toolbar: [{ name: "create" , text : "Add Sample Code" }],
columns: [
{ field: "SampleCode", title: "Sample Codes" },
{ command: ["edit", "destroy"], title: " ", width: "210px" }
]
});
I have added some comments inside the code so that you get some idea about my problem. I just need to know how to approach validation for duplicates using the custom validator that calls a dataSource for the same purpose.
Any help will be greatly appreciated.