Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 1b59793

Browse files
committedSep 23, 2014
feat(form): support dynamic form validation
1 parent 2e32125 commit 1b59793

File tree

2 files changed

+49
-4
lines changed

2 files changed

+49
-4
lines changed
 

‎src/ng/directive/form.js

+15-4
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,8 @@ SUBMITTED_CLASS = 'ng-submitted';
5252
*
5353
*/
5454
//asks for $scope to fool the BC controller module
55-
FormController.$inject = ['$element', '$attrs', '$scope', '$animate'];
56-
function FormController(element, attrs, $scope, $animate) {
55+
FormController.$inject = ['$element', '$attrs', '$scope', '$animate', '$interpolate'];
56+
function FormController(element, attrs, $scope, $animate, $interpolate) {
5757
var form = this,
5858
parentForm = element.parent().controller('form') || nullFormCtrl,
5959
controls = [];
@@ -62,7 +62,7 @@ function FormController(element, attrs, $scope, $animate) {
6262
form.$error = {};
6363
form.$$success = {};
6464
form.$pending = undefined;
65-
form.$name = attrs.name || attrs.ngForm;
65+
form.$name = $interpolate(attrs.name || attrs.ngForm || '')($scope);
6666
form.$dirty = false;
6767
form.$pristine = true;
6868
form.$valid = true;
@@ -485,10 +485,21 @@ var formDirectiveFactory = function(isNgForm) {
485485
}
486486

487487
var parentFormCtrl = formElement.parent().controller('form'),
488-
alias = attr.name || attr.ngForm;
488+
alias = controller.$name;
489489

490490
if (alias) {
491491
setter(scope, alias, controller, alias);
492+
attr.$observe(attr.name ? 'name' : 'ngForm', function(newValue) {
493+
if (alias === newValue) return;
494+
setter(scope, alias, undefined, alias);
495+
alias = newValue;
496+
setter(scope, alias, controller, alias);
497+
if (parentFormCtrl) {
498+
parentFormCtrl.$$renameControl(controller, alias);
499+
} else {
500+
controller.$name = alias;
501+
}
502+
});
492503
}
493504
if (parentFormCtrl) {
494505
formElement.on('$destroy', function() {

‎test/ng/directive/formSpec.js

+34
Original file line numberDiff line numberDiff line change
@@ -782,6 +782,40 @@ describe('form', function() {
782782
});
783783
});
784784

785+
786+
it('should rename nested form controls when interpolated name changes', function() {
787+
scope.idA = 'A';
788+
scope.idB = 'X';
789+
790+
doc = $compile(
791+
'<form name="form">' +
792+
'<div ng-form="nested{{idA}}">' +
793+
'<div ng-form name="nested{{idB}}"' +
794+
'</div>' +
795+
'</div>' +
796+
'</form'
797+
)(scope);
798+
799+
scope.$digest();
800+
var formA = scope.form.nestedA;
801+
expect(formA).toBeDefined();
802+
expect(formA.$name).toBe('nestedA');
803+
804+
var formX = formA.nestedX;
805+
expect(formX).toBeDefined();
806+
expect(formX.$name).toBe('nestedX');
807+
808+
scope.idA = 'B';
809+
scope.idB = 'Y';
810+
scope.$digest();
811+
812+
expect(scope.form.nestedA).toBeUndefined();
813+
expect(scope.form.nestedB).toBe(formA);
814+
expect(formA.nestedX).toBeUndefined();
815+
expect(formA.nestedY).toBe(formX);
816+
});
817+
818+
785819
describe('$setSubmitted', function() {
786820
beforeEach(function() {
787821
doc = $compile(

0 commit comments

Comments
 (0)
This repository has been archived.