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

fix(form): allow dynamic form names which initially evaluate to blank #11096

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
28 changes: 14 additions & 14 deletions src/ng/directive/form.js
Original file line number Diff line number Diff line change
Expand Up @@ -454,10 +454,12 @@ var formDirectiveFactory = function(isNgForm) {
name: 'form',
restrict: isNgForm ? 'EAC' : 'E',
controller: FormController,
compile: function ngFormCompile(formElement) {
compile: function ngFormCompile(formElement, attr) {
// Setup initial state of the control
formElement.addClass(PRISTINE_CLASS).addClass(VALID_CLASS);

var nameAttr = attr.name ? 'name' : (isNgForm && attr.ngForm ? 'ngForm' : false);

return {
pre: function ngFormPreLink(scope, formElement, attr, controller) {
// if `action` attr is not present on the form, prevent the default action (submission)
Expand Down Expand Up @@ -488,23 +490,21 @@ var formDirectiveFactory = function(isNgForm) {
});
}

var parentFormCtrl = controller.$$parentForm,
alias = controller.$name;

if (alias) {
setter(scope, alias, controller, alias);
attr.$observe(attr.name ? 'name' : 'ngForm', function(newValue) {
if (alias === newValue) return;
setter(scope, alias, undefined, alias);
alias = newValue;
setter(scope, alias, controller, alias);
parentFormCtrl.$$renameControl(controller, alias);
var parentFormCtrl = controller.$$parentForm;

if (nameAttr) {
setter(scope, controller.$name, controller, controller.$name);
attr.$observe(nameAttr, function(newValue) {
if (controller.$name === newValue) return;
setter(scope, controller.$name, undefined, controller.$name);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I realize this line was there before - but as far as I can tell, it doesn't do anything, because two lines after the setter is called again, this time with the correct value (also, all tests pass if I remove this)

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is to remove the controller from the scope under the old name (although it doesn't actually remove it but sets it to undefined). I've added this expectation to the form renaming tests.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, yeah, that makes a lot of sense.

parentFormCtrl.$$renameControl(controller, newValue);
setter(scope, controller.$name, controller, controller.$name);
});
}
formElement.on('$destroy', function() {
parentFormCtrl.$removeControl(controller);
if (alias) {
setter(scope, alias, undefined, alias);
if (nameAttr) {
setter(scope, attr[nameAttr], undefined, controller.$name);
}
extend(controller, nullFormCtrl); //stop propagating child destruction handlers upwards
});
Expand Down
24 changes: 21 additions & 3 deletions test/ng/directive/formSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -881,20 +881,38 @@ describe('form', function() {

it('should rename forms with no parent when interpolated name changes', function() {
var element = $compile('<form name="name{{nameID}}"></form>')(scope);
var element2 = $compile('<div ng-form="name{{nameID}}"></div>')(scope);
var element2 = $compile('<div ng-form="ngform{{nameID}}"></div>')(scope);
scope.nameID = "A";
scope.$digest();
var form = element.controller('form');
var form2 = element2.controller('form');
expect(scope.nameA).toBe(form);
expect(scope.ngformA).toBe(form2);
expect(form.$name).toBe('nameA');
expect(form2.$name).toBe('nameA');
expect(form2.$name).toBe('ngformA');

scope.nameID = "B";
scope.$digest();
expect(scope.nameA).toBeUndefined();
expect(scope.ngformA).toBeUndefined();
expect(scope.nameB).toBe(form);
expect(scope.ngformB).toBe(form2);
expect(form.$name).toBe('nameB');
expect(form2.$name).toBe('nameB');
expect(form2.$name).toBe('ngformB');
});

it('should rename forms with an initially blank name', function() {
var element = $compile('<form name="{{name}}"></form>')(scope);
scope.$digest();
var form = element.controller('form');
expect(scope['']).toBe(form);
expect(form.$name).toBe('');
scope.name = 'foo';
scope.$digest();
expect(scope.foo).toBe(form);
expect(form.$name).toBe('foo');
expect(scope.foo).toBe(form);
});

describe('$setSubmitted', function() {
beforeEach(function() {
Expand Down