Skip to content

Commit 6ce417e

Browse files
committed
feat(input): support dynamic element validation
Interpolates the form controll attribute name, so that dynamic form controls (such as those rendered in an ngRepeat) will always have their expected interpolated name. Closes angular#4791
1 parent ace40d5 commit 6ce417e

File tree

2 files changed

+31
-3
lines changed

2 files changed

+31
-3
lines changed

src/ng/directive/input.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -1657,8 +1657,8 @@ var VALID_CLASS = 'ng-valid',
16571657
*
16581658
*
16591659
*/
1660-
var NgModelController = ['$scope', '$exceptionHandler', '$attrs', '$element', '$parse', '$animate', '$timeout', '$rootScope', '$q',
1661-
function($scope, $exceptionHandler, $attr, $element, $parse, $animate, $timeout, $rootScope, $q) {
1660+
var NgModelController = ['$scope', '$exceptionHandler', '$attrs', '$element', '$parse', '$animate', '$timeout', '$rootScope', '$q', '$interpolate',
1661+
function($scope, $exceptionHandler, $attr, $element, $parse, $animate, $timeout, $rootScope, $q, $interpolate) {
16621662
this.$viewValue = Number.NaN;
16631663
this.$modelValue = Number.NaN;
16641664
this.$validators = {};
@@ -1675,7 +1675,7 @@ var NgModelController = ['$scope', '$exceptionHandler', '$attrs', '$element', '$
16751675
this.$error = {}; // keep invalid keys here
16761676
this.$$success = {}; // keep valid keys here
16771677
this.$pending = undefined; // keep pending keys here
1678-
this.$name = $attr.name;
1678+
this.$name = $interpolate($attr.name || '', false)($scope);
16791679

16801680

16811681
var parsedNgModel = $parse($attr.ngModel),

test/ng/directive/inputSpec.js

+28
Original file line numberDiff line numberDiff line change
@@ -1289,6 +1289,34 @@ describe('input', function() {
12891289
}
12901290
}));
12911291

1292+
1293+
it('should interpolate input names (string)', function() {
1294+
scope.nameID = '47';
1295+
compileInput('<input type="text" ng-model="name" name="name{{nameID}}" />');
1296+
expect(scope.form.name47.$pristine).toBeTruthy();
1297+
changeInputValueTo('caitp');
1298+
expect(scope.form.name47.$dirty).toBeTruthy();
1299+
});
1300+
1301+
1302+
it('should interpolate input names (number)', function() {
1303+
scope.nameID = 47;
1304+
compileInput('<input type="text" ng-model="name" name="name{{nameID}}" />');
1305+
expect(scope.form.name47.$pristine).toBeTruthy();
1306+
changeInputValueTo('caitp');
1307+
expect(scope.form.name47.$dirty).toBeTruthy();
1308+
});
1309+
1310+
1311+
it('should interpolate input names (undefined)', function() {
1312+
scope.nameID = undefined;
1313+
compileInput('<input type="text" ng-model="name" name="name{{nameID}}" />');
1314+
expect(scope.form.name.$pristine).toBeTruthy();
1315+
changeInputValueTo('caitp');
1316+
expect(scope.form.name.$dirty).toBeTruthy();
1317+
});
1318+
1319+
12921320
describe('"change" event', function() {
12931321
function assertBrowserSupportsChangeEvent(inputEventSupported) {
12941322
// Force browser to report a lack of an 'input' event

0 commit comments

Comments
 (0)