8

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

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 that my form wouldn't submit if the datepicker field wasn't touched (even if it had data in it, e.g. when I would edit a resource). It basically counted the form as invalid even though a good date was submitted. This directive fixed that, but when I upgraded my angular to 1.3 the directive no longer works.

What would I need to change to get this directive to work again?

6
  • did you upgrade angular-ui also?
    – charlietfl
    Commented Apr 27, 2015 at 11:45
  • 2
    What exactly doesn't work? Any errors in console? Create some simple plunker that reproduces your problem. Commented Apr 27, 2015 at 13:24
  • As I said my form won't submit because it is treated as invalid (even though it is valid, only it isn't touched). As for angular-ui, the latest version is for angular 1.2, so no version for 1.3 yet.
    – NLuburić
    Commented Apr 28, 2015 at 11:18
  • @user2352164 I suggest you to check $scope.form object to found what exactly makes the form invalid.
    – BotanMan
    Commented May 8, 2015 at 12:58
  • Have you already checked the migration guide? There are quite a few breaking changes in 1.3, but they're (mostly) well documented in the guide.
    – Tiddo
    Commented May 12, 2015 at 18:43

1 Answer 1

0

Here you can find something about a breaking change that affects ngModel in 1.3:

Since the HTML5 pattern validation constraint validates the input value, we should also validate against the viewValue. While this worked in core up to Angular 1.2, in 1.3, we changed not only validation, but the way input[date] and input[number] are handled - they parse their input values into Date and Number respectively, which cannot be validated by a regex ...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.