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

Commit 28a1bf9

Browse files
feat(form): add ability to reset a form to its pristine state
1 parent a8b0400 commit 28a1bf9

File tree

4 files changed

+81
-1
lines changed

4 files changed

+81
-1
lines changed

src/ng/directive/form.js

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ var nullFormCtrl = {
55
$addControl: noop,
66
$removeControl: noop,
77
$setValidity: noop,
8-
$setDirty: noop
8+
$setDirty: noop,
9+
$setPristine: noop
910
};
1011

1112
/**
@@ -119,6 +120,31 @@ function FormController(element, attrs) {
119120
form.$pristine = false;
120121
};
121122

123+
/**
124+
* @ngdoc function
125+
* @name ng.directive:form.FormController#$setPristine
126+
* @methodOf ng.directive:form.FormController
127+
*
128+
* @description
129+
* Sets the form to its pristine state.
130+
*
131+
* This method can be called to remove the 'ng-dirty' class and set the form to its pristine
132+
* state (ng-pristine class). Calling this method will also affect all the controls contained in
133+
* this form.
134+
*
135+
* Setting a form back to a pristine state is often useful when we want to 'reuse' a form after
136+
* saving or resetting it.
137+
*/
138+
form.$setPristine = function () {
139+
element.removeClass(DIRTY_CLASS).addClass(PRISTINE_CLASS);
140+
form.$dirty = false;
141+
form.$pristine = true;
142+
angular.forEach(form, function(value, key) {
143+
if (value.$name && value.$setPristine) {
144+
value.$setPristine();
145+
}
146+
});
147+
};
122148
}
123149

124150

src/ng/directive/input.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -947,6 +947,22 @@ var NgModelController = ['$scope', '$exceptionHandler', '$attrs', '$element', '$
947947
parentForm.$setValidity(validationErrorKey, isValid, this);
948948
};
949949

950+
/**
951+
* @ngdoc function
952+
* @name ng.directive:ngModel.NgModelController#$setPristine
953+
* @methodOf ng.directive:ngModel.NgModelController
954+
*
955+
* @description
956+
* Sets the control to its pristine state.
957+
*
958+
* This method can be called to remove the 'ng-dirty' class and set the control to its pristine
959+
* state (ng-pristine class).
960+
*/
961+
this.$setPristine = function () {
962+
this.$dirty = false;
963+
this.$pristine = true;
964+
$element.removeClass(DIRTY_CLASS).addClass(PRISTINE_CLASS);
965+
}
950966

951967
/**
952968
* @ngdoc function

test/ng/directive/formSpec.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -327,4 +327,30 @@ describe('form', function() {
327327
expect(doc).toBeDirty();
328328
});
329329
});
330+
331+
describe('set pristine state', function() {
332+
333+
beforeEach(function() {
334+
doc = $compile(
335+
'<form name="form">' +
336+
'<input ng-model="name" name="name" store-model-ctrl/>' +
337+
'</form>')(scope);
338+
339+
scope.$digest();
340+
});
341+
342+
it('should set form and controls to the pristine state', function() {
343+
344+
var form = scope.form, input = doc.find('input').eq(0)
345+
346+
control.$setViewValue('');
347+
scope.$apply();
348+
expect(doc).toBeDirty();
349+
350+
form.$setPristine();
351+
expect(doc).toBePristine();
352+
expect(input).toBePristine();
353+
expect(control.$pristine).toBeTruthy();
354+
});
355+
});
330356
});

test/ng/directive/inputSpec.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,18 @@ describe('NgModelController', function() {
117117
});
118118
});
119119

120+
describe('setPristine', function() {
121+
122+
it('should set control to its pristine state', function() {
123+
ctrl.$setViewValue('edit');
124+
expect(ctrl.$dirty).toBe(true);
125+
expect(ctrl.$pristine).toBe(false);
126+
127+
ctrl.$setPristine();
128+
expect(ctrl.$dirty).toBe(false);
129+
expect(ctrl.$pristine).toBe(true);
130+
});
131+
});
120132

121133
describe('view -> model', function() {
122134

0 commit comments

Comments
 (0)