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?
javascript angularjs twitter-bootstrap datepicker
user2352164
source share