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

Commit 953ee22

Browse files
shahatapetebacondarwin
authored andcommitted
fix(ngForm): don't clear validity of whole form when removing control
Calling `$$clearControlValidity` on the parent of a nested form caused the parent form to look like there are no more errors on the nested form even if it still had some inputs with errors. there is no need to call this method recursively since `$setValidity` will propagate the new validity state well enough. Closes #8863
1 parent c3064f7 commit 953ee22

File tree

2 files changed

+29
-2
lines changed

2 files changed

+29
-2
lines changed

src/ng/directive/form.js

-2
Original file line numberDiff line numberDiff line change
@@ -160,8 +160,6 @@ function FormController(element, attrs, $scope, $animate) {
160160
function clear(queue, validationToken) {
161161
form.$setValidity(validationToken, true, control);
162162
}
163-
164-
parentForm.$$clearControlValidity(form);
165163
};
166164

167165
form.$$setPending = function(validationToken, control) {

test/ng/directive/formSpec.js

+29
Original file line numberDiff line numberDiff line change
@@ -494,6 +494,35 @@ describe('form', function() {
494494
expect(doc.find('div').hasClass('ng-valid-required')).toBe(true);
495495
});
496496

497+
it('should leave the parent form invalid when deregister a removed input', function() {
498+
doc = jqLite(
499+
'<form name="parent">' +
500+
'<div class="ng-form" name="child">' +
501+
'<input ng-if="inputPresent" ng-model="modelA" name="inputA" required>' +
502+
'<input ng-model="modelB" name="inputB" required>' +
503+
'</div>' +
504+
'</form>');
505+
$compile(doc)(scope);
506+
scope.inputPresent = true;
507+
scope.$apply();
508+
509+
var parent = scope.parent,
510+
child = scope.child,
511+
inputA = child.inputA,
512+
inputB = child.inputB;
513+
514+
expect(parent).toBeDefined();
515+
expect(child).toBeDefined();
516+
expect(parent.$error.required).toEqual([child]);
517+
expect(child.$error.required).toEqual([inputB, inputA]);
518+
519+
//remove child input
520+
scope.inputPresent = false;
521+
scope.$apply();
522+
523+
expect(parent.$error.required).toEqual([child]);
524+
expect(child.$error.required).toEqual([inputB]);
525+
});
497526

498527
it('should chain nested forms in repeater', function() {
499528
doc = jqLite(

0 commit comments

Comments
 (0)