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

Commit

Permalink
feat(formController): add $setUntouched to propagate untouched state
Browse files Browse the repository at this point in the history
Closes #9050
  • Loading branch information
shahata authored and caitp committed Sep 23, 2014
1 parent d8c8b2e commit fd89975
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 0 deletions.
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

1 comment on commit fd89975

@bobber205
Copy link

Choose a reason for hiding this comment

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

best feature ever. got to remove

      _.each($scope.form, function(formFieldObject, name) {
          if (name.indexOf("$") == -1) {
            formFieldObject.$touched = false;
          }
      });

Please sign in to comment.