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

Commit 59487c8

Browse files
committed
fix(resetModel): Updated resetModel to set the form to pristine
Updated the resetModel function to set call on the form as well as the fields #574
1 parent 8648c16 commit 59487c8

File tree

2 files changed

+41
-1
lines changed

2 files changed

+41
-1
lines changed

src/directives/formly-field.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,10 @@ function formlyField($http, $q, $compile, $templateCache, $interpolate, formlyCo
197197
resetFormControl($scope.options.formControl)
198198
}
199199
}
200+
if ($scope.form) {
201+
$scope.form.$setUntouched && $scope.form.$setUntouched()
202+
$scope.form.$setPristine()
203+
}
200204
}
201205

202206
function resetFormControl(formControl, isMultiNgModel) {

src/directives/formly-field.test.js

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1309,7 +1309,6 @@ describe('formly-field', function() {
13091309
// initial state
13101310
expect(field.formControl.$dirty).to.be.false
13111311
expect(field.formControl.$touched).to.be.false
1312-
13131312
// modification
13141313
scope.model.foo = '~=[,,_,,]:3'
13151314
field.formControl.$setTouched()
@@ -1427,6 +1426,43 @@ describe('formly-field', function() {
14271426
expect(() => field.resetModel()).to.not.throw()
14281427
})
14291428

1429+
it('should reset the form state on the input and form both', () => {
1430+
const field = getNewField({key: 'foo'})
1431+
scope.fields = [field]
1432+
compileAndDigest(`
1433+
<form name="theForm">
1434+
<formly-form form="theForm" model="model" fields="fields" options="options"></formly-form>
1435+
</form>
1436+
`)
1437+
// initial state
1438+
expect(field.formControl.$dirty).to.be.false
1439+
expect(field.formControl.$touched).to.be.false
1440+
expect(scope.theForm.$dirty).to.be.false
1441+
expect(scope.theForm.$pristine).to.be.true
1442+
// modification
1443+
scope.model.foo = '~=[,,_,,]:3'
1444+
field.formControl.$setTouched()
1445+
field.formControl.$setDirty()
1446+
scope.$digest()
1447+
1448+
// expect modification
1449+
expect(field.formControl.$dirty).to.be.true
1450+
expect(field.formControl.$touched).to.be.true
1451+
expect(scope.theForm.$dirty).to.be.true
1452+
expect(scope.theForm.$pristine).to.be.false
1453+
expect(field.formControl.$modelValue).to.eq('~=[,,_,,]:3')
1454+
1455+
// reset state
1456+
field.resetModel()
1457+
1458+
// expect reset
1459+
expect(field.formControl.$modelValue).to.be.empty
1460+
expect(field.formControl.$touched).to.be.false
1461+
expect(field.formControl.$dirty).to.be.false
1462+
expect(scope.theForm.$dirty).to.be.false
1463+
expect(scope.theForm.$pristine).to.be.true
1464+
})
1465+
14301466
it(`should not digest if there's a digest in progress`, () => {
14311467
scope.fields = [getNewField()]
14321468
compileAndDigest()

0 commit comments

Comments
 (0)