From 57c8e7facebeeba6d24f52cecdf029d268c5358e Mon Sep 17 00:00:00 2001 From: amdi-odoo Date: Fri, 25 Nov 2022 08:06:24 +0000 Subject: [PATCH] [FIX] web: prevents not dirty forms saving Purpose ======= Prevents auto save of not dirty forms with text fields on page reload and tab/browser closing. Specifications ============== The text field default value is making Odoo consider the form as dirty even when it's not which triggers auto saving for not dirty forms. Change the text field widget setDirty condition to prevent considering empty textarea string dirty. Typically, if the previous value was false, false and empty string should be considered as the same value. Task-3031570 closes odoo/odoo#106619 X-original-commit: 787ef58dd3f91f829509f738ee40c8764c403757 Signed-off-by: Thibault Delavallee (tde) --- .../src/views/fields/input_field_hook.js | 2 +- .../tests/views/form/form_view_tests.js | 34 +++++++++++++++++++ 2 files changed, 35 insertions(+), 1 deletion(-) diff --git a/addons/web/static/src/views/fields/input_field_hook.js b/addons/web/static/src/views/fields/input_field_hook.js index 030cb50b411..7eb9d3824e3 100644 --- a/addons/web/static/src/views/fields/input_field_hook.js +++ b/addons/web/static/src/views/fields/input_field_hook.js @@ -147,7 +147,7 @@ export function useInputField(params) { return; } - if (val !== component.props.value) { + if ((val || false) !== (component.props.value || false)) { await component.props.update(val); lastSetValue = inputRef.el.value; if (component.props.setDirty) { diff --git a/addons/web/static/tests/views/form/form_view_tests.js b/addons/web/static/tests/views/form/form_view_tests.js index bcd205e3249..45c0437e905 100644 --- a/addons/web/static/tests/views/form/form_view_tests.js +++ b/addons/web/static/tests/views/form/form_view_tests.js @@ -11670,6 +11670,40 @@ QUnit.module("Views", (hooks) => { assert.verifySteps([], "should not save because we do not change anything"); }); + QUnit.test("Auto save: save on closing tab/browser (not dirty) with text field", async function (assert) { + + serverData.models.partner.fields.bloup = { + string: "Bloup", + type: "text", + default: false, + }; + + await makeView({ + type: "form", + resModel: "partner", + serverData, + arch: ` +
+ + + +
`, + resId: 1, + mockRPC(route, { args, method, model }) { + if (method === "write" && model === "partner") { + assert.step("save"); // should not be called + } + }, + }); + + assert.strictEqual(target.querySelector(".o_field_widget[name=bloup] textarea").value, "", "should contain the default value"); + + window.dispatchEvent(new Event("beforeunload")); + await nextTick(); + + assert.verifySteps([], "should not save because we do not change anything"); + }); + QUnit.test("Auto save: save on closing tab/browser (detached form)", async function (assert) { serverData.actions[1] = { id: 1,