Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

fix(ngModel): let aliased validator directives work on any element #12658

Closed
wants to merge 1 commit into from
Closed
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
5 changes: 2 additions & 3 deletions src/jqLite.js
Original file line number Diff line number Diff line change
Expand Up @@ -556,9 +556,8 @@ function getBooleanAttrName(element, name) {
return booleanAttr && BOOLEAN_ELEMENTS[nodeName_(element)] && booleanAttr;
}

function getAliasedAttrName(element, name) {
var nodeName = element.nodeName;
return (nodeName === 'INPUT' || nodeName === 'TEXTAREA') && ALIASED_ATTR[name];
function getAliasedAttrName(name) {
return ALIASED_ATTR[name];
}

forEach({
Expand Down
2 changes: 1 addition & 1 deletion src/ng/compile.js
Original file line number Diff line number Diff line change
Expand Up @@ -1077,7 +1077,7 @@ function $CompileProvider($provide, $$sanitizeUriProvider) {

var node = this.$$element[0],
booleanKey = getBooleanAttrName(node, key),
aliasedKey = getAliasedAttrName(node, key),
aliasedKey = getAliasedAttrName(key),
observer = key,
nodeName;

Expand Down
73 changes: 73 additions & 0 deletions test/ng/directive/validatorsSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,24 @@ describe('validators', function() {
expect($rootScope.form.test.$modelValue).toBe('12340');
expect(inputElm).toBeValid();
});


it('should validate on non-input elements', inject(function($compile) {
$rootScope.pattern = '\\d{4}';
var elm = $compile('<span ng-model="value" pattern="\\d{4}"></span>')($rootScope);
var elmNg = $compile('<span ng-model="value" ng-pattern="pattern"></span>')($rootScope);
var ctrl = elm.controller('ngModel');
var ctrlNg = elmNg.controller('ngModel');

expect(ctrl.$error.pattern).not.toBe(true);
expect(ctrlNg.$error.pattern).not.toBe(true);

ctrl.$setViewValue('12');
ctrlNg.$setViewValue('12');

expect(ctrl.$error.pattern).toBe(true);
expect(ctrlNg.$error.pattern).toBe(true);
}));
});


Expand Down Expand Up @@ -283,6 +301,24 @@ describe('validators', function() {
helper.changeInputValueTo('12345');
expect(ctrl.$isEmpty).toHaveBeenCalledWith('12345');
});


it('should validate on non-input elements', inject(function($compile) {
$rootScope.min = 3;
var elm = $compile('<span ng-model="value" minlength="{{min}}"></span>')($rootScope);
var elmNg = $compile('<span ng-model="value" ng-minlength="min"></span>')($rootScope);
var ctrl = elm.controller('ngModel');
var ctrlNg = elmNg.controller('ngModel');

expect(ctrl.$error.minlength).not.toBe(true);
expect(ctrlNg.$error.minlength).not.toBe(true);

ctrl.$setViewValue('12');
ctrlNg.$setViewValue('12');

expect(ctrl.$error.minlength).toBe(true);
expect(ctrlNg.$error.minlength).toBe(true);
}));
});


Expand Down Expand Up @@ -453,6 +489,24 @@ describe('validators', function() {
helper.changeInputValueTo('12345');
expect(ctrl.$isEmpty).toHaveBeenCalledWith('12345');
});


it('should validate on non-input elements', inject(function($compile) {
$rootScope.max = 3;
var elm = $compile('<span ng-model="value" maxlength="{{max}}"></span>')($rootScope);
var elmNg = $compile('<span ng-model="value" ng-maxlength="max"></span>')($rootScope);
var ctrl = elm.controller('ngModel');
var ctrlNg = elmNg.controller('ngModel');

expect(ctrl.$error.maxlength).not.toBe(true);
expect(ctrlNg.$error.maxlength).not.toBe(true);

ctrl.$setViewValue('1234');
ctrlNg.$setViewValue('1234');

expect(ctrl.$error.maxlength).toBe(true);
expect(ctrlNg.$error.maxlength).toBe(true);
}));
});


Expand Down Expand Up @@ -547,6 +601,7 @@ describe('validators', function() {
expect(inputElm).toBeValid();
});


it('should validate emptiness against the viewValue', function() {
var inputElm = helper.compileInput('<input type="text" name="input" ng-model="value" required />');

Expand All @@ -560,5 +615,23 @@ describe('validators', function() {
helper.changeInputValueTo('12345');
expect(ctrl.$isEmpty).toHaveBeenCalledWith('12345');
});


it('should validate on non-input elements', inject(function($compile) {
$rootScope.value = '12';
var elm = $compile('<span ng-model="value" required></span>')($rootScope);
var elmNg = $compile('<span ng-model="value" ng-required="true"></span>')($rootScope);
var ctrl = elm.controller('ngModel');
var ctrlNg = elmNg.controller('ngModel');

expect(ctrl.$error.required).not.toBe(true);
expect(ctrlNg.$error.required).not.toBe(true);

ctrl.$setViewValue('');
ctrlNg.$setViewValue('');

expect(ctrl.$error.required).toBe(true);
expect(ctrlNg.$error.required).toBe(true);
}));
});
});