Skip to content

Commit

Permalink
fix(ngModel): consider ngMin/ngMax values when validating number inpu…
Browse files Browse the repository at this point in the history
…t types

With this fix ngModel will treat ngMin as a min error and ngMax as a max error.
This also means that when either of these two values is changed then ngModel will
revaliate itself.
  • Loading branch information
matsko committed Sep 4, 2014
1 parent 7b273a2 commit 25541c1
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 2 deletions.
2 changes: 2 additions & 0 deletions src/jqLite.js
Expand Up @@ -506,6 +506,8 @@ forEach('input,select,option,textarea,button,form,details'.split(','), function(
var ALIASED_ATTR = {
'ngMinlength' : 'minlength',
'ngMaxlength' : 'maxlength',
'ngMin' : 'min',
'ngMax' : 'max',
'ngPattern' : 'pattern'
};

Expand Down
4 changes: 2 additions & 2 deletions src/ng/directive/input.js
Expand Up @@ -1128,7 +1128,7 @@ function numberInputType(scope, element, attr, ctrl, $sniffer, $browser) {
return value;
});

if (attr.min) {
if (attr.min || attr.ngMin) {
var minVal;
ctrl.$validators.min = function(value) {
return ctrl.$isEmpty(value) || isUndefined(minVal) || value >= minVal;
Expand All @@ -1144,7 +1144,7 @@ function numberInputType(scope, element, attr, ctrl, $sniffer, $browser) {
});
}

if (attr.max) {
if (attr.max || attr.ngMax) {
var maxVal;
ctrl.$validators.max = function(value) {
return ctrl.$isEmpty(value) || isUndefined(maxVal) || value <= maxVal;
Expand Down
82 changes: 82 additions & 0 deletions test/ng/directive/inputSpec.js
Expand Up @@ -2919,6 +2919,47 @@ describe('input', function() {
});
});

describe('ngMin', function() {

it('should validate', function() {
compileInput('<input type="number" ng-model="value" name="alias" ng-min="50" />');

changeInputValueTo('1');
expect(inputElm).toBeInvalid();
expect(scope.value).toBeFalsy();
expect(scope.form.alias.$error.min).toBeTruthy();

changeInputValueTo('100');
expect(inputElm).toBeValid();
expect(scope.value).toBe(100);
expect(scope.form.alias.$error.min).toBeFalsy();
});

it('should validate even if the ngMin value changes on-the-fly', function() {
scope.min = 10;
compileInput('<input type="number" ng-model="value" name="alias" ng-min="min" />');

changeInputValueTo('15');
expect(inputElm).toBeValid();

scope.min = 20;
scope.$digest();
expect(inputElm).toBeInvalid();

scope.min = null;
scope.$digest();
expect(inputElm).toBeValid();

scope.min = '20';
scope.$digest();
expect(inputElm).toBeInvalid();

scope.min = 'abc';
scope.$digest();
expect(inputElm).toBeValid();
});
});


describe('max', function() {

Expand Down Expand Up @@ -2961,6 +3002,47 @@ describe('input', function() {
});
});

describe('ngMax', function() {

it('should validate', function() {
compileInput('<input type="number" ng-model="value" name="alias" ng-max="5" />');

changeInputValueTo('20');
expect(inputElm).toBeInvalid();
expect(scope.value).toBeUndefined();
expect(scope.form.alias.$error.max).toBeTruthy();

changeInputValueTo('0');
expect(inputElm).toBeValid();
expect(scope.value).toBe(0);
expect(scope.form.alias.$error.max).toBeFalsy();
});

it('should validate even if the ngMax value changes on-the-fly', function() {
scope.max = 10;
compileInput('<input type="number" ng-model="value" name="alias" ng-max="max" />');

changeInputValueTo('5');
expect(inputElm).toBeValid();

scope.max = 0;
scope.$digest();
expect(inputElm).toBeInvalid();

scope.max = null;
scope.$digest();
expect(inputElm).toBeValid();

scope.max = '4';
scope.$digest();
expect(inputElm).toBeInvalid();

scope.max = 'abc';
scope.$digest();
expect(inputElm).toBeValid();
});
});


describe('required', function() {

Expand Down

0 comments on commit 25541c1

Please sign in to comment.