Hi,
i am trying to use kendo validator for displaying validation messages, after clicking of update button if user not selects any field it's displaying validation as expected.
once user selects any values on Name field validation message is disappearing properly, but in countries field even after selecting it's still showing error message.
can you please help me
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Index</title>
<link href="~/NewCSS/kendo.mobile.all.min.css" rel="stylesheet" />
<link rel="stylesheet" href="https://kendo.cdn.telerik.com/2020.1.114/styles/kendo.default-v2.min.css" />
<script type="text/javascript" src="https://code.jquery.com/jquery-1.12.4.min.js"></script>
<script type="text/javascript" src="~/scripts/angular.js"></script>
<script type="text/javascript" src="~/scripts/angular.js"></script>
<script src="~/scripts/angular.min.js"></script>
<script type="text/javascript" src="https://kendo.cdn.telerik.com/2020.1.114/js/kendo.all.min.js"></script>
<style>
.k-grid td {
white-space: normal
}
.breakWord20 {
word-break: break-all !important;
word-wrap: break-word !important;
vertical-align: top;
}
.k-grid-header .k-header {
overflow: visible !important;
white-space: normal !important;
}
</style>
<script type="text/javascript">
var app = angular.module("MyApp", ["kendo.directives"]);
app.controller("MyController", function ($scope, $window, $http) {
$scope.mainGridOptions = {
dataSource: {
//type: "json",
transport: {
read: { url: "/home/GetCustomers" },
create: {
url: "/home/InsertCustomers",
},
},
pageSize: 2,
schema: {
model: {
id: "Id",
fields: {
Id: { editable: false, nullable: true, type: "number" },
Name: { editable: true, nullable: true, type: "string" },
countries: { editable: true, nullable: true, type: "object" },
}
}
},
serverPaging: true,
serverSorting: true
},
editable: "inline",
sortable: true,
pageable: true,
resizeable: true,
columns: [
{ field: "Id", title: "Id", width: "50px" },
{
field: "Name", title: "Name"
},
{
title: "Country",
field: "countries",
width: "200px",
headerAttributes: { style: "white-space: normal", "class": "breakWord20" },
defaultValue: Customers,
template: "#=CountryName(countries) #",
editor: function (container) {
var input = $('<input id="countries" name="countries" />');
input.appendTo(container);
input.kendoDropDownTree({
autoClose: true,
checkboxes: true,
checkAll: true,
placeholder: "Select ...",
height: "auto",
dataTextField: "Name",
dataValueField: "CountryId",
dataSource: Countries,
dataBound: onDataBound,
}).appendTo(container);
}
},
{
title: 'Action',
command: [{ name: "edit", text: "Edit", iconClass: "k-icon k-i-hyperlink-open" }]
}
],
toolbar: ["create"],
};
GetCountries();
})
//validations
$(document).ready(function () {
$.extend(true, kendo.ui.validator, {
rules: {
CountryValidation: function (input, params) {
if (input.is("[name='countries']")) {
var grid = input.closest(".k-grid").data("kendoGrid");
var dataItem = grid.dataItem($(".k-grid-edit-row"));
return input.val().length != 0;
}
return true;
},
LimitValidation: function (input, params) {
if (input.is("[name='Name']")) {
var grid = input.closest(".k-grid").data("kendoGrid");
var dataItem = grid.dataItem($(".k-grid-edit-row"));
return input.val() != "";
}
return true;
},
},
messages: {
CountryValidation: "Country is mandatory",
LimitValidation: "Name is mandatory",
}
});
});
var selectedItem;
function onDataBound(e) {
if (selectedItem) {
setTimeout(function () {
e.sender.value(selectedItem);
e.sender.trigger('change')
});
}
}
var Customers = [
{ Name: "Please select", CountryId: 0 },
];
var Countries = [];
function GetCountries() {
$.ajax({
method: 'Get',
url: '/home/GetCountryName',
}).success(function (data, status, headers, config) {
Countries = data;
}).error(function (data, status, headers, config) {
message = 'Unexpected Error';
});
}
function CountryName(countries) {
if (countries != null)
return Array.prototype.map.call(countries, function (item) { return item.Name; }).join("/")
}
</script>
</head>
<body ng-app="MyApp" ng-controller="MyController">
<kendo-grid k-options="mainGridOptions"></kendo-grid>
</body>
</html>