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

Commit 64abf70

Browse files
committed
feat(form): support dynamic form validation
1 parent b1644aa commit 64abf70

File tree

2 files changed

+49
-3
lines changed

2 files changed

+49
-3
lines changed

src/ng/directive/form.js

+15-3
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;
@@ -467,9 +467,21 @@ var formDirectiveFactory = function(isNgForm) {
467467

468468
var parentFormCtrl = formElement.parent().controller('form'),
469469
alias = attr.name || attr.ngForm;
470+
var unobserveName = noop;
470471

471472
if (alias) {
472473
setter(scope, alias, controller, alias);
474+
unobserveName = attr.$observe(attr.name ? 'name' : 'ngForm', function(newValue) {
475+
if (alias === newValue) return;
476+
setter(scope, alias, undefined, alias);
477+
alias = newValue;
478+
setter(scope, alias, controller, alias);
479+
if (parentFormCtrl) {
480+
parentFormCtrl.$$renameControl(controller, alias);
481+
} else {
482+
controller.$name = alias;
483+
}
484+
});
473485
}
474486
if (parentFormCtrl) {
475487
formElement.on('$destroy', function() {

test/ng/directive/formSpec.js

+34
Original file line numberDiff line numberDiff line change
@@ -750,6 +750,40 @@ describe('form', function() {
750750
});
751751
});
752752

753+
754+
iit('should rename nested form controls when interpolated name changes', function() {
755+
scope.idA = 'A';
756+
scope.idB = 'X';
757+
758+
doc = $compile(
759+
'<form name="form">' +
760+
'<div ng-form="nested{{idA}}">' +
761+
'<div ng-form name="nested{{idB}}"' +
762+
'</div>' +
763+
'</div>' +
764+
'</form'
765+
)(scope);
766+
767+
scope.$digest();
768+
var formA = scope.form.nestedA;
769+
expect(formA).toBeDefined();
770+
expect(formA.$name).toBe('nestedA');
771+
772+
var formX = formA.nestedX;
773+
expect(formX).toBeDefined();
774+
expect(formX.$name).toBe('nestedX');
775+
776+
scope.idA = 'B';
777+
scope.idB = 'Y';
778+
scope.$digest();
779+
780+
expect(scope.form.nestedA).toBeUndefined();
781+
expect(scope.form.nestedB).toBe(formA);
782+
expect(formA.nestedX).toBeUndefined();
783+
expect(formA.nestedY).toBe(formX);
784+
});
785+
786+
753787
describe('$setSubmitted', function() {
754788
beforeEach(function() {
755789
doc = $compile(

0 commit comments

Comments
 (0)