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

feat(formController): add $setUntouched to propagate untouched state #9050

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
19 changes: 19 additions & 0 deletions src/ng/directive/form.js
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,25 @@ function FormController(element, attrs, $scope, $animate) {
});
};

/**
* @ngdoc method
* @name form.FormController#$setUntouched
*
* @description
* Sets the form to its untouched state.
*
* This method can be called to remove the 'ng-touched' class and set the form controls to their
* untouched state (ng-untouched class).
*
* Setting a form controls back to their untouched state is often useful when setting the form
* back to its pristine state.
*/
form.$setUntouched = function () {
forEach(controls, function(control) {
control.$setUntouched();
});
};

/**
* @ngdoc method
* @name form.FormController#$setSubmitted
Expand Down
32 changes: 32 additions & 0 deletions test/ng/directive/formSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -750,6 +750,38 @@ describe('form', function() {
});
});

describe('$setUntouched', function () {
it('should trigger setUntouched on form controls', function() {
var form = $compile(
'<form name="myForm">' +
'<input name="alias" type="text" ng-model="name" />' +
'</form>')(scope);
scope.$digest();

scope.myForm.alias.$setTouched();
expect(scope.myForm.alias.$touched).toBe(true);
scope.myForm.$setUntouched();
expect(scope.myForm.alias.$touched).toBe(false);
dealoc(form);
});

it('should trigger setUntouched on form controls with nested forms', function() {
var form = $compile(
'<form name="myForm">' +
'<div class="ng-form" name="childForm">' +
'<input name="alias" type="text" ng-model="name" />' +
'</div>' +
'</form>')(scope);
scope.$digest();

scope.myForm.childForm.alias.$setTouched();
expect(scope.myForm.childForm.alias.$touched).toBe(true);
scope.myForm.$setUntouched();
expect(scope.myForm.childForm.alias.$touched).toBe(false);
dealoc(form);
});
});

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