Last active
March 2, 2016 14:08
-
-
Save xie-qianyue/d23df03e433276ece820 to your computer and use it in GitHub Desktop.
Angular Directive
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
hdrDirectives.directive('hdrMaxOneWeekAfter', ['dateServices', function (dateServices) { | |
'use strict'; | |
return { | |
require: 'ngModel', | |
restrict: 'A', | |
link: function (scope, elm, attrs, ctrl) { | |
var valider = function (viewValue) { | |
// pour ça marche, le ngModel correspondant doit etre 'formaData.**' | |
var greaterDate = scope.formData[attrs.hdrMaxOneWeekAfter], | |
greaterDateCtrl = angular.element(document.querySelector('[name=' + attrs.hdrMaxOneWeekAfter + ']')).controller('ngModel'); | |
// on ne verifie l'interval que les dates concernes sont valides | |
if (!dateServices.isDate(ctrl.$modelValue) || !dateServices.isDate(greaterDate)) { | |
ctrl.$setValidity('hdrInOneWeek', true); | |
} else { | |
if (dateServices.isDatesValides(ctrl.$modelValue, greaterDate)) { | |
ctrl.$setValidity('hdrDatesValid', true); | |
greaterDateCtrl.$setValidity('hdrDatesValid', true); | |
if (dateServices.isIntervalSuperieurNombreJ(ctrl.$modelValue, greaterDate, 7)) { | |
ctrl.$setValidity('hdrInOneWeek', true); | |
greaterDateCtrl.$setValidity('hdrInOneWeek', true); | |
} else { | |
ctrl.$setValidity('hdrInOneWeek', false); | |
} | |
} else { | |
ctrl.$setValidity('hdrDatesValid', false); | |
} | |
} | |
return viewValue; | |
}; | |
ctrl.$parsers.unshift(valider); | |
ctrl.$formatters.push(valider); | |
} | |
}; | |
}]); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment