Angular update from 1.2 to 1.3 violated my directive

I have the following directive that I put in input fields that use the angular -ui-bootstrap datepicker parameter:

angular.module('directives.validators.date', []) .directive('validDate',[ '$filter', function ($filter) { return { restrict:'A', require:'ngModel', link: function (scope, el, attrs, ngModel) { var pattern = /^(0[1-9]|[12][0-9]|3[01])\.(0[1-9]|1[012])\.(19|20)\d\d$/; ngModel.scope = scope; ngModel.attrs = attrs; el.on('blur',function() { if(typeof ngModel.$viewValue === "object"){ var str = $filter('date')(ngModel.$viewValue, "dd.MM.yyyy"); ngModel.$setViewValue(str); } if(ngModel.$viewValue){ if(ngModel.$viewValue!=="" && !pattern.test(ngModel.$viewValue)){ ngModel.$setValidity("date",false); } } }); scope.$watch(function () { return ngModel.$modelValue; }, function() { if(ngModel.$pristine){ //if the data has just been fetched, convert it to date format. if (typeof ngModel.$viewValue === "number"){ var date = new Date(ngModel.$viewValue); //var str = $filter('date')(date, "dd.MM.yyyy"); ngModel.$setViewValue(date); ngModel.$setPristine(true); var formName = $("form")[0].name; ngModel.scope[formName].$setPristine(true); ngModel.$setValidity("date",true); } } if(ngModel.$viewValue){ //when the filed is changed, if it is corrected set that the date is valid if(ngModel.$viewValue==="" || pattern.test(ngModel.$viewValue)){ ngModel.$setValidity("date",true); } } }); } }; }]); 

I had a problem with the datepicker component, which my form would not have presented if the datepicker field was not affected (even if it had data, for example, when I edited the resource). He basically considered the form invalid, even if a good date was sent. This directive fixed this, but when I updated my angular to 1.3, the directive no longer works.

What do I need to change to re-enable this directive?

+8
javascript angularjs twitter-bootstrap datepicker
source share
1 answer

Here you can find something about a violation that affects ngModel in version 1.3:

Since the validation restriction of the HTML5 template validates the input value, we also need to validate it on viewValue. Although this worked the kernel before Angular 1.2, in 1.3, we changed not only the verification, but the input[date] and input[number] paths are also processed - they analyze their input values ​​in Date and Number accordingly, which cannot be confirmed by a regular expression .. .

0
source share

All Articles