Last active
August 29, 2015 14:18
-
-
Save raphaelrodrigues/ee6830f51b1ca3b8dd7a to your computer and use it in GitHub Desktop.
directive to validate 2 dates
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
.directive('lowerThan', [ | |
function() { | |
var link = function($scope, $element, $attrs, ctrl) { | |
var validate = function(viewValue) { | |
var comparisonModel = $attrs.lowerThan; | |
var t, f; | |
if (!viewValue || !comparisonModel) { | |
// It's valid because we have nothing to compare against | |
ctrl.$setValidity('lowerThan', true); | |
} | |
if (comparisonModel) { | |
var to = comparisonModel.split("-"); | |
t = new Date(to[2], to[1] - 1, to[0]); | |
} | |
if (viewValue) { | |
var from = viewValue.split("-"); | |
f = new Date(from[2], from[1] - 1, from[0]); | |
} | |
ctrl.$setValidity('lowerThan', Date.parse(t) > Date.parse(f)); | |
// It's valid if model is lower than the model we're comparing against | |
return viewValue; | |
}; | |
ctrl.$parsers.unshift(validate); | |
//ctrl.$formatters.push(validate); | |
}; | |
return { | |
require: 'ngModel', | |
link: link | |
}; | |
} | |
]).directive('higherThan', [ | |
function() { | |
var link = function($scope, $element, $attrs, ctrl) { | |
var validate = function(viewValue) { | |
var comparisonModel = $attrs.higherThan; | |
var t, f; | |
if (!viewValue || !comparisonModel) { | |
// It's valid because we have nothing to compare against | |
ctrl.$setValidity('higherThan', true); | |
} | |
if (comparisonModel) { | |
var to = comparisonModel.split("-"); | |
t = new Date(to[2], to[1] - 1, to[0]); | |
} | |
if (viewValue) { | |
var from = viewValue.split("-"); | |
f = new Date(from[2], from[1] - 1, from[0]); | |
} | |
ctrl.$setValidity('higherThan', Date.parse(t) < Date.parse(f)); | |
// It's valid if model is higher than the model we're comparing against | |
return viewValue; | |
}; | |
ctrl.$parsers.unshift(validate); | |
//ctrl.$formatters.push(validate); | |
}; | |
return { | |
require: 'ngModel', | |
link: link | |
}; | |
} | |
]); |
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
<form name="form" > | |
Min: <input name="min" type="text" ng-model="field.min" lower-than="{{field.max}}" /> | |
<span class="error" ng-show="form.min.$error.lowerThan"> | |
Min cannot exceed max. {{form.min.$error.lowerThan}} | |
</span> | |
<br /> | |
Max: <input name="max" type="text" ng-model="field.max" higher-than="{{field.min}}" /> | |
<span class="error" ng-show="form.max.$error.higherThan"> | |
Max cannot be lower than min. {{form.max.$error.higherThan}} | |
</span> | |
</form> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment