From c93eb01ec4b01c7529baafaa31b52dd39f29fbf2 Mon Sep 17 00:00:00 2001 From: Lucas Perais Date: Fri, 7 Oct 2022 14:54:37 +0000 Subject: [PATCH] [FIX] web: form: no "oh snap" dialog on error in dialog Have a form view in a dialog (action in target=new) The create/write of that model throws an error. Before this commit, the "Oh snap!" dialog was displayed, but makes little sense in a dialog. After this commit, the old UserError dialog is displayed. closes odoo/odoo#102764 X-original-commit: d36f1708219f63fae5a86b6c1e81b328ca96b347 Signed-off-by: Aaron Bohy (aab) --- .../static/src/views/form/form_controller.js | 2 +- .../tests/views/form/form_view_tests.js | 56 ++++++++++++++++++- 2 files changed, 56 insertions(+), 2 deletions(-) diff --git a/addons/web/static/src/views/form/form_controller.js b/addons/web/static/src/views/form/form_controller.js index dde612e9c0c..8d834872154 100644 --- a/addons/web/static/src/views/form/form_controller.js +++ b/addons/web/static/src/views/form/form_controller.js @@ -360,7 +360,7 @@ export class FormController extends Component { async beforeExecuteActionButton(clickParams) { if (clickParams.special !== "cancel") { return this.model.root - .save({ stayInEdition: true, useSaveErrorDialog: true }) + .save({ stayInEdition: true, useSaveErrorDialog: !this.env.inDialog }) .then((saved) => { if (saved && this.props.onSave) { this.props.onSave(this.model.root); 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 e38667fd061..85081405add 100644 --- a/addons/web/static/tests/views/form/form_view_tests.js +++ b/addons/web/static/tests/views/form/form_view_tests.js @@ -39,6 +39,7 @@ import { localization } from "@web/core/l10n/localization"; import { SIZES } from "@web/core/ui/ui_service"; import { errorService } from "@web/core/errors/error_service"; import { RPCError } from "@web/core/network/rpc_service"; +import { WarningDialog } from "@web/core/errors/error_dialogs"; import * as legacyCore from "web.core"; @@ -9355,7 +9356,10 @@ QUnit.module("Views", (hooks) => { }, }); - assert.hasClass(target.querySelector('button[data-value="4"]'), "o_arrow_button_current"); + assert.hasClass( + target.querySelector('button[data-value="4"]'), + "o_arrow_button_current" + ); assert.hasClass(target.querySelector('button[data-value="4"]'), "disabled"); failFlag = true; @@ -11968,6 +11972,56 @@ QUnit.module("Views", (hooks) => { assert.containsOnce(target, ".o_form_error_dialog"); }); + QUnit.test("no 'oh snap' error in form view in dialog", async (assert) => { + assert.expect(5); + + registry.category("services").add("error", errorService); + registry.category("error_dialogs").add("odoo.exceptions.UserError", WarningDialog); + // remove the override in qunit.js that swallows unhandledrejection errors + // s.t. we let the error service handle them + const originalOnUnhandledRejection = window.onunhandledrejection; + window.onunhandledrejection = () => {}; + registerCleanup(() => { + window.onunhandledrejection = originalOnUnhandledRejection; + }); + + serverData.views = { + "partner,false,form": `
`, + }; + + const webClient = await createWebClient({ + serverData, + mockRPC(route, { method }) { + if (method === "create") { + assert.step("create"); + const error = new RPCError("Some business message"); + error.data = { context: {} }; + error.exceptionName = "odoo.exceptions.UserError"; + throw error; + } + }, + }); + + await doAction(webClient, { + type: "ir.actions.act_window", + target: "new", + res_model: "partner", + view_mode: "form", + views: [[false, "form"]], + }); + + await editInput(target, ".o_field_widget[name='foo'] input", "test"); + await click(target.querySelector(".modal footer .myButton")); + assert.verifySteps(["create"]); + await nextTick(); + assert.containsNone(target, ".o_form_error_dialog"); + assert.containsN(target, ".modal", 2); + assert.strictEqual( + target.querySelectorAll(".modal .modal-body")[1].textContent, + "Some business message" + ); + }); + QUnit.test('field "length" with value 0: can apply onchange', async function (assert) { serverData.models.partner.fields.length = { string: "Length", type: "float", default: 0 }; serverData.models.partner.fields.foo.default = "foo default";