Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 45 additions & 0 deletions ngMax/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
define(['angular'], function (angular) {
'use strict';

return angular.module('app.ng-max', []).directive('ngMax', function () {
return {
restrict: 'A',
require: 'ngModel',
link: function (scope, element, attr, ctrl) {

scope.initialize = function () {
scope.$watch(attr.ngMax, function () {
ctrl.$setViewValue(ctrl.$viewValue);
});

ctrl.$parsers.push( scope.maxValidator );
ctrl.$formatters.push( scope.maxValidator );
};

/**
* Returns the value and sets validity in true if value didn't surpass the upper limit.
* Otherwise sets validity in false and returns undefined.
*/
scope.maxValidator = function (value) {
var max = scope.$eval(attr.ngMax) || Infinity;
if (!scope.isValidable(value) && value > max) {
ctrl.$setValidity('ngMax', false);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will it be better to use max as validation key? As an example angular ng-minlength directive uses minlength key.

return undefined;
} else {
ctrl.$setValidity('ngMax', true);
return value;
}
};

/**
* Returns true if value is not something we can validate against max.
*/
scope.isValidable = function (value) {
return angular.isUndefined(value) || value === '' || value === null || value !== value;
};

scope.initialize();
}
};
});
});
45 changes: 45 additions & 0 deletions ngMin/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
define(['angular'], function (angular) {
'use strict';

return angular.module('app.ng-min', []).directive('ngMin', function () {
return {
restrict: 'A',
require: 'ngModel',
link: function (scope, element, attr, ctrl) {

scope.initialize = function () {
scope.$watch(attr.ngMin, function () {
ctrl.$setViewValue(ctrl.$viewValue);
});

ctrl.$parsers.push( scope.minValidator );
ctrl.$formatters.push( scope.minValidator );
};

/**
* Returns the value and sets validity in true if value didn't surpass the upper limit.
* Otherwise sets validity in false and returns undefined.
*/
scope.minValidator = function (value) {
var min = scope.$eval(attr.ngMin) || 0;
if (!scope.isValidable(value) && value < min) {
ctrl.$setValidity('ngMin', false);
return undefined;
} else {
ctrl.$setValidity('ngMin', true);
return value;
}
};

/**
* Returns true if value is not something we can validate against min.
*/
scope.isValidable = function (value) {
return angular.isUndefined(value) || value === '' || value === null || value !== value;
};

scope.initialize();
}
};
});
});