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

Commit fd89975

Browse files
shahatacaitp
authored andcommitted
feat(formController): add $setUntouched to propagate untouched state
Closes #9050
1 parent d8c8b2e commit fd89975

File tree

2 files changed

+51
-0
lines changed

2 files changed

+51
-0
lines changed

src/ng/directive/form.js

+19
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,25 @@ function FormController(element, attrs, $scope, $animate) {
230230
});
231231
};
232232

233+
/**
234+
* @ngdoc method
235+
* @name form.FormController#$setUntouched
236+
*
237+
* @description
238+
* Sets the form to its untouched state.
239+
*
240+
* This method can be called to remove the 'ng-touched' class and set the form controls to their
241+
* untouched state (ng-untouched class).
242+
*
243+
* Setting a form controls back to their untouched state is often useful when setting the form
244+
* back to its pristine state.
245+
*/
246+
form.$setUntouched = function () {
247+
forEach(controls, function(control) {
248+
control.$setUntouched();
249+
});
250+
};
251+
233252
/**
234253
* @ngdoc method
235254
* @name form.FormController#$setSubmitted

test/ng/directive/formSpec.js

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

753+
describe('$setUntouched', function () {
754+
it('should trigger setUntouched on form controls', function() {
755+
var form = $compile(
756+
'<form name="myForm">' +
757+
'<input name="alias" type="text" ng-model="name" />' +
758+
'</form>')(scope);
759+
scope.$digest();
760+
761+
scope.myForm.alias.$setTouched();
762+
expect(scope.myForm.alias.$touched).toBe(true);
763+
scope.myForm.$setUntouched();
764+
expect(scope.myForm.alias.$touched).toBe(false);
765+
dealoc(form);
766+
});
767+
768+
it('should trigger setUntouched on form controls with nested forms', function() {
769+
var form = $compile(
770+
'<form name="myForm">' +
771+
'<div class="ng-form" name="childForm">' +
772+
'<input name="alias" type="text" ng-model="name" />' +
773+
'</div>' +
774+
'</form>')(scope);
775+
scope.$digest();
776+
777+
scope.myForm.childForm.alias.$setTouched();
778+
expect(scope.myForm.childForm.alias.$touched).toBe(true);
779+
scope.myForm.$setUntouched();
780+
expect(scope.myForm.childForm.alias.$touched).toBe(false);
781+
dealoc(form);
782+
});
783+
});
784+
753785
describe('$setSubmitted', function() {
754786
beforeEach(function() {
755787
doc = $compile(

0 commit comments

Comments
 (0)