Skip to content

Commit

Permalink
Stop parsing when value is invalid
Browse files Browse the repository at this point in the history
  • Loading branch information
timmipetit committed Jul 30, 2018
1 parent 5dd2a70 commit 7f9df84
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 9 deletions.
2 changes: 1 addition & 1 deletion bower.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "angular-moment-duration",
"version": "0.0.8",
"version": "0.0.9",
"homepage": "https://github.com/Recras/angular-moment-duration",
"description": "An AngularJS directive for moment.js duration",
"main": "./src/angular-moment-duration.js",
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "angular-moment-duration",
"version": "0.0.8",
"version": "0.0.9",
"description": "An AngularJS directive for moment.js duration",
"main": "src/angular-moment-duration.js",
"scripts": {
Expand Down
21 changes: 14 additions & 7 deletions src/angular-moment-duration.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,18 +61,25 @@ angular.module('ui.moment-duration', [])
element.val(Math.floor(value));
};

ngModel.$viewChangeListeners.push(function() {
var valid = true;
if (attrs.min && parseInt(ngModel.$viewValue, 10) < parseInt(attrs.min, 10)) {
valid = false;
} else if (attrs.max && parseInt(ngModel.$viewValue, 10) > parseInt(attrs.max, 10)) {
valid = false;
var isValid = function(value) {
if (attrs.min && parseInt(value, 10) < parseInt(attrs.min, 10)) {
return false;
}
if (attrs.max && parseInt(value, 10) > parseInt(attrs.max, 10)) {
return false;
}
ngModel.$setValidity('min', valid);
return true;
};

ngModel.$viewChangeListeners.push(function() {
ngModel.$setValidity('min', isValid(ngModel.$viewValue));
});

ngModel.$parsers.unshift(function(viewValue) {
var duration = moment.duration(ngModel.$modelValue);
if (!isValid(viewValue)) {
return duration;
}
var newValue;
if (scope.maxUnit === undefined) {
newValue = duration.get(scope.type);
Expand Down

0 comments on commit 7f9df84

Please sign in to comment.