From fd8997551f9ed4431f5e99d61f637139485076b9 Mon Sep 17 00:00:00 2001 From: Shahar Talmi Date: Fri, 12 Sep 2014 15:17:32 +0300 Subject: [PATCH] feat(formController): add $setUntouched to propagate untouched state Closes #9050 --- src/ng/directive/form.js | 19 +++++++++++++++++++ test/ng/directive/formSpec.js | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 51 insertions(+) diff --git a/src/ng/directive/form.js b/src/ng/directive/form.js index ba1a6a210f8d..3ee544ea2543 100644 --- a/src/ng/directive/form.js +++ b/src/ng/directive/form.js @@ -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 diff --git a/test/ng/directive/formSpec.js b/test/ng/directive/formSpec.js index 57f67f752313..f843801d2735 100644 --- a/test/ng/directive/formSpec.js +++ b/test/ng/directive/formSpec.js @@ -750,6 +750,38 @@ describe('form', function() { }); }); + describe('$setUntouched', function () { + it('should trigger setUntouched on form controls', function() { + var form = $compile( + '
' + + '' + + '
')(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( + '
' + + '
' + + '' + + '
' + + '
')(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(