From 75718e40759e1dec26bfb2448dd796dfbb5d00ad Mon Sep 17 00:00:00 2001 From: alexandraRamanenka Date: Fri, 10 Nov 2023 14:43:27 +0200 Subject: [PATCH] FIO-7547: Container hidden with conditional logic still appears in submission --- .../_classes/nested/NestedComponent.js | 2 +- src/components/container/Container.unit.js | 22 +++++++++- src/components/container/fixtures/comp4.js | 43 +++++++++++++++++++ src/components/container/fixtures/index.js | 3 +- 4 files changed, 67 insertions(+), 3 deletions(-) create mode 100644 src/components/container/fixtures/comp4.js diff --git a/src/components/_classes/nested/NestedComponent.js b/src/components/_classes/nested/NestedComponent.js index c2697a031e..dfee7555a1 100644 --- a/src/components/_classes/nested/NestedComponent.js +++ b/src/components/_classes/nested/NestedComponent.js @@ -617,7 +617,7 @@ export default class NestedComponent extends Field { clearOnHide(show) { super.clearOnHide(show); if (this.component.clearOnHide) { - if (this.allowData && !this.hasValue()) { + if (this.allowData && !this.hasValue() && !(this.options.server && !this.visible)) { this.dataValue = this.defaultValue; } if (this.hasValue()) { diff --git a/src/components/container/Container.unit.js b/src/components/container/Container.unit.js index 0cebca0600..ee17fc74e9 100644 --- a/src/components/container/Container.unit.js +++ b/src/components/container/Container.unit.js @@ -6,7 +6,8 @@ import ContainerComponent from './Container'; import { comp1, comp2, - comp3 + comp3, + comp4, } from './fixtures'; import { Formio } from '../../Formio'; @@ -77,4 +78,23 @@ describe('Container Component', () => { }, 100); }).catch(done); }); + + it('Should not set the default value when clearOnHide during the server-side validation', (done) => { + const form = _.cloneDeep(comp4); + const element = document.createElement('div'); + + Formio.createForm(element, form, { server: true, noDefaults: true }).then(form => { + form.setValue({ data: { checkbox: false } }, { + sanitize: true, + }, true); + + form.checkConditions(); + form.clearOnHide(); + + setTimeout(() => { + assert.deepEqual(form._data, { checkbox: false }, 'Should not add Container\'s key'); + done(); + }, 200); + }).catch(done); + }); }); diff --git a/src/components/container/fixtures/comp4.js b/src/components/container/fixtures/comp4.js new file mode 100644 index 0000000000..c48b94e359 --- /dev/null +++ b/src/components/container/fixtures/comp4.js @@ -0,0 +1,43 @@ +export default { + type: 'form', + display: 'form', + components: [ + { + label: 'Checkbox', + tableView: false, + key: 'checkbox', + type: 'checkbox', + input: true, + }, + { + label: 'Container', + tableView: false, + key: 'container', + conditional: { + show: true, + when: 'checkbox', + eq: 'true', + }, + type: 'container', + input: true, + components: [ + { + label: 'Text Field', + applyMaskOn: 'change', + tableView: true, + key: 'textField', + type: 'textfield', + input: true, + }, + ], + }, + { + type: 'button', + label: 'Submit', + key: 'submit', + disableOnInvalid: true, + input: true, + tableView: false, + }, + ], +}; diff --git a/src/components/container/fixtures/index.js b/src/components/container/fixtures/index.js index 63f18da5cf..f1a58d4b03 100644 --- a/src/components/container/fixtures/index.js +++ b/src/components/container/fixtures/index.js @@ -1,4 +1,5 @@ import comp1 from './comp1'; import comp2 from './comp2'; import comp3 from './comp3'; -export { comp1, comp2, comp3 }; +import comp4 from './comp4'; +export { comp1, comp2, comp3, comp4 };