简体   繁体   English

Kendo AngularJS DatePicker自定义验证

[英]Kendo AngularJS DatePicker custom validation

How do I get custom validation working for the Kendo Angular directive DatePicker ? 如何为Kendo Angular指令DatePicker进行自定义验证? I want to implement the following example but using the Angular directive version of the DatePicker : 我想实现以下示例,但使用DatePickerAngular指令版本:

KendoUI DatePicker validation when multiple date fields are on a page 当页面上有多个日期字段时,KendoUI DatePicker验证

Any help would be appreciated. 任何帮助,将不胜感激。

This is how I implemented a custom Datepicker based on KEndo's Datepicker with my own template and validation : 这就是我使用自己的模板和验证实现基于KEndo的Datepicker的自定义Datepicker的方法:

 appModule.directive('nemoDatePicker', function () { return { restrict: 'E', require: '?ngModel', scope: { 'name': '@', 'text': '@', 'ngModel': '=', }, templateUrl: '/App/nemo/nemoDatePicker.html', link: function (scope, element, attrs, ngModel) { }, controller: function ($scope) { $scope.error = function (name) { var s = $scope.editor[name]; return $scope.editor.$invalid && $scope.editor.$dirty ? "has-error" : ""; }; } }; }); appModule.directive('kendoDateValidator', ['$sce', function ($sce) { return { restrict: 'A', // only activate on element attribute require: '?ngModel', // get a hold of NgModelController link: function (scope, element, attrs, ngModel) { if (!ngModel) return; // do nothing if no ng-model // Specify how UI should be updated ngModel.$render = function () { //element.html($sce.getTrustedHtml(ngModel.$viewValue || '')); }; // Listen for change events to enable binding element.on('blur keyup change', function () { scope.$evalAsync(read); }); read(); // initialize function read() { if (!element.val()) return; const startTime = performance.now(); if (!isDate(element.val())) { //console.log(element.val() + " bad"); ngModel.$setValidity('date', false); } else { //console.log(element.val() + " good"); ngModel.$setValidity('date', true); } const duration = performance.now() - startTime; //console.log("read took " + duration + " ms"); } function isDate(x) { var d = kendo.parseDate(x, "dd/MM/yyyy"); return d instanceof Date; }; } }; }]); 
 <div ng-form="editor"> <div class="Box" ng-class="error(name)"> <div class="SubTitle"> <div class="DirectiveIcon1"> <span class="glyphicon glyphicon-calendar"></span> </div> {{text}} </div> <input kendo-date-picker culture="he-IL" k-format="'dd/MM/yyyy'" id="{{name}}" name="{{name}}" ng-model="ngModel" required kendo-Date-Validator class="form-control"/> <div> <div> <span class="help-block" ng-show="editor.$error.required && editor.$dirty">שדה חובה</span> <span class="help-block" ng-show="editor.$error.date">יש להזין תאריך</span> </div> </div> </div> </div> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM