mirror of
https://github.com/zhigang1992/angular.js.git
synced 2026-05-26 10:35:15 +08:00
fix(NgModel): make sure the ngMinlength and ngMaxlength validators use the $validators pipeline
Fixes #6304
This commit is contained in:
@@ -1004,23 +1004,17 @@ function textInputType(scope, element, attr, ctrl, $sniffer, $browser) {
|
||||
// min length validator
|
||||
if (attr.ngMinlength) {
|
||||
var minlength = int(attr.ngMinlength);
|
||||
var minLengthValidator = function(value) {
|
||||
return validate(ctrl, 'minlength', ctrl.$isEmpty(value) || value.length >= minlength, value);
|
||||
ctrl.$validators.minlength = function(value) {
|
||||
return ctrl.$isEmpty(value) || value.length >= minlength;
|
||||
};
|
||||
|
||||
ctrl.$parsers.push(minLengthValidator);
|
||||
ctrl.$formatters.push(minLengthValidator);
|
||||
}
|
||||
|
||||
// max length validator
|
||||
if (attr.ngMaxlength) {
|
||||
var maxlength = int(attr.ngMaxlength);
|
||||
var maxLengthValidator = function(value) {
|
||||
return validate(ctrl, 'maxlength', ctrl.$isEmpty(value) || value.length <= maxlength, value);
|
||||
ctrl.$validators.maxlength = function(value) {
|
||||
return ctrl.$isEmpty(value) || value.length <= maxlength;
|
||||
};
|
||||
|
||||
ctrl.$parsers.push(maxLengthValidator);
|
||||
ctrl.$formatters.push(maxLengthValidator);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1345,14 +1345,14 @@ describe('input', function() {
|
||||
|
||||
describe('minlength', function() {
|
||||
|
||||
it('should invalid shorter than given minlength', function() {
|
||||
it('should invalidate values that are shorter than the given minlength', function() {
|
||||
compileInput('<input type="text" ng-model="value" ng-minlength="3" />');
|
||||
|
||||
changeInputValueTo('aa');
|
||||
expect(scope.value).toBeUndefined();
|
||||
expect(inputElm).toBeInvalid();
|
||||
|
||||
changeInputValueTo('aaa');
|
||||
expect(scope.value).toBe('aaa');
|
||||
expect(inputElm).toBeValid();
|
||||
});
|
||||
|
||||
it('should listen on ng-minlength when minlength is observed', function() {
|
||||
@@ -1373,14 +1373,14 @@ describe('input', function() {
|
||||
|
||||
describe('maxlength', function() {
|
||||
|
||||
it('should invalid shorter than given maxlength', function() {
|
||||
it('should invalidate values that are longer than the given maxlength', function() {
|
||||
compileInput('<input type="text" ng-model="value" ng-maxlength="5" />');
|
||||
|
||||
changeInputValueTo('aaaaaaaa');
|
||||
expect(scope.value).toBeUndefined();
|
||||
expect(inputElm).toBeInvalid();
|
||||
|
||||
changeInputValueTo('aaa');
|
||||
expect(scope.value).toBe('aaa');
|
||||
expect(inputElm).toBeValid();
|
||||
});
|
||||
|
||||
it('should listen on ng-maxlength when maxlength is observed', function() {
|
||||
|
||||
Reference in New Issue
Block a user