Hi,

I'm using Angular 1.5.5.  I'm working on my first custom directive that 
checks if a date falls within an array of allowed years.  If it does not, 
it displays a custom error.  I think I'm close, but running into problems 
overriding the "Please Fix this Field" message with my custom message. 
 Currently, I use jQuery to override it (which I know is not preferred and 
it doesn't quite work as expected).  Below is my directive.  Can someone 
help me figure out the correct way to change the "Please Fix this Field" 
message? - thanks:

WellnessReimbursementsApp.directive('dateValidator', function () {
    return {
        restrict: 'A',
        require: 'ngModel',
        link: function (scope, element, attrs, controller) {
            var dateEntered;
            scope.$watch(attrs.ngModel, function (newVal, oldVal, scope) {
                dateEntered = newVal;
                check();
            });

            var check = function () {
                if (typeof dateEntered === 'undefined') {
                    return;
                }
                
                if (!validate(dateEntered)) {
                    dateEntered = new Date(dateEntered);
                    if (!validate(dateEntered)) {
                        return;
                    }
                }
  
                var foundValidYear = false;
                var validYears = '';
                $.each(scope.member.AllowedReimbursementYears, function 
(index, value) {
                    if (dateEntered.getFullYear() == value) {
                        foundValidYear = true;
                    }
                    validYears += validYears == '' ? value : ' or ' + value;
                });

                if (foundValidYear) {
                    controller.$setValidity('dateValidator', true);
                }
                else {
                    controller.$setValidity('dateValidator', false);
                    var errMsg = dateEntered.getFullYear() + ' 
reimbursments are not allowed.  Date completed must be in ' + validYears + 
'.';
                    element.parent().find('.bs-invalid-msg').html(errMsg);
                }

                return;
            };

            var validate = function (date) {
                if (Object.prototype.toString.call(date) === '[object 
Date]') {
                    if (isNaN(date.getTime())) {
                        return false;
                    }
                    else {
                        return true;
                    }
                }
                else {
                    return false;
                }
            };

        }
    }


});


-- 
You received this message because you are subscribed to the Google Groups 
"Angular" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/angular.
For more options, visit https://groups.google.com/d/optout.

Reply via email to