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

Commit cc67dd7

Browse files
committed
5th try is the charm, actually fixes #432 with some additional tests to ensure that it is functioning as expected. If the form state was dirty it doesn't reset the form to pristine. If the form was pristine. It manually sets it back to pristine after initializing the $viewValue with the formatted view. -🐙
1 parent c5dcb4d commit cc67dd7

File tree

2 files changed

+36
-1
lines changed

2 files changed

+36
-1
lines changed

src/directives/formly-field.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -360,6 +360,7 @@ function formlyField($http, $q, $compile, $templateCache, $interpolate, formlyCo
360360
function addFormatters() {
361361
setParsersOrFormatters('formatters');
362362
const ctrl = scope.fc;
363+
const formWasPristine = scope.form.$pristine;
363364
if (scope.options.formatters) {
364365
let value = ctrl.$modelValue;
365366
ctrl.$formatters.forEach((formatter) => {
@@ -369,6 +370,9 @@ function formlyField($http, $q, $compile, $templateCache, $interpolate, formlyCo
369370
ctrl.$setViewValue(value);
370371
ctrl.$render();
371372
ctrl.$setPristine();
373+
if (formWasPristine) {
374+
scope.form.$setPristine();
375+
}
372376
}
373377
}
374378

src/directives/formly-field.test.js

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1015,7 +1015,7 @@ describe('formly-field', function() {
10151015
expect(ctrl.$viewValue).to.equal('hello! boo!');
10161016
});
10171017

1018-
it(`should format a model value right from the start and the form should still be pristine`, () => {
1018+
it(`should format a model value right from the start and the controller should still be pristine`, () => {
10191019
scope.model = {myKey: 'hello'};
10201020
scope.fields = [getNewField({
10211021
key: 'myKey',
@@ -1030,6 +1030,37 @@ describe('formly-field', function() {
10301030
expect(ctrl.$pristine).to.equal(true);
10311031
});
10321032

1033+
it(`should format a model value on initilization and keep the form state dirty if it was already dirty`, () => {
1034+
scope.model = {myKey: 'hello'};
1035+
scope.fields = [getNewField({
1036+
key: 'myKey',
1037+
formatters: ['"!" + $viewValue + "!"']
1038+
})];
1039+
compileAndDigest();
1040+
scope.theForm.$setDirty();
1041+
1042+
const ctrl = getNgModelCtrl();
1043+
1044+
expect(ctrl.$viewValue).to.equal('!hello!');
1045+
expect(scope.theForm.$dirty).to.equal(true);
1046+
1047+
});
1048+
1049+
it(`should format a model value on initilization and keep the form state pristine if it was already pristine`, () => {
1050+
scope.model = {myKey: 'hello'};
1051+
scope.fields = [getNewField({
1052+
key: 'myKey',
1053+
formatters: ['"!" + $viewValue + "!"']
1054+
})];
1055+
compileAndDigest();
1056+
1057+
const ctrl = getNgModelCtrl();
1058+
1059+
expect(ctrl.$viewValue).to.equal('!hello!');
1060+
expect(scope.theForm.$pristine).to.equal(true);
1061+
1062+
});
1063+
10331064
it.skip(`should handle multiple form controllers when formatting a model value right from the start`, () => {
10341065
scope.model = {
10351066
multiNgModel: {

0 commit comments

Comments
 (0)