You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Odoo技术问题:如何按条件阻止向导表单关闭?

阻止Odoo向导表单关闭的实现方案

要实现点击关闭按钮或按下Esc键时,根据条件(如指定字段不为空)阻止向导关闭,可通过重写Odoo前端组件的关闭逻辑实现,以下是具体方案:

通用全局拦截方法

该方法对所有向导生效,适合全局需求:

1. 创建自定义JavaScript文件

在模块的static/src/js/目录下新建wizard_close_block.js,内容如下:

odoo.define('your_module.wizard_close_control', function (require) {
    "use strict";

    const FormDialog = require('web.FormDialog');

    FormDialog.include({
        // 拦截关闭按钮点击事件
        async _onClose() {
            const currentRecord = this.model.get(this.handle);
            // 替换为你的判断条件:例如此处检查target_field字段是否不为空
            if (currentRecord.data.target_field) {
                this.displayNotification({
                    type: 'warning',
                    message: '目标字段不为空,无法关闭向导!',
                });
                return; // 终止关闭流程
            }
            // 条件不满足时执行原关闭逻辑
            return this._super(...arguments);
        },
        // 拦截Esc按键事件
        async _handleEscape() {
            const currentRecord = this.model.get(this.handle);
            if (currentRecord.data.target_field) {
                this.displayNotification({
                    type: 'warning',
                    message: '目标字段不为空,无法关闭向导!',
                });
                return;
            }
            return this._super(...arguments);
        },
    });

    return FormDialog;
});

2. 在模块配置中引入JS文件

修改模块根目录下的__manifest__.py,在assets节点添加该JS文件:

'assets': {
    'web.assets_backend': [
        'your_module/static/src/js/wizard_close_block.js',
    ],
},

特定向导单独拦截方法

若仅需对某个特定向导生效,可在该向导的XML视图中嵌入脚本:

<odoo>
    <record id="your_wizard_form_view_id" model="ir.ui.view">
        <field name="name">your.wizard.form</field>
        <field name="model">your.wizard.model</field>
        <field name="arch" type="xml">
            <form string="Your Wizard">
                <!-- 向导表单内容 -->
                <field name="target_field"/>
                
                <!-- 嵌入拦截脚本 -->
                <t t-js="true">
                    odoo.define('your_module.specific_wizard_close', function (require) {
                        "use strict";
                        const FormController = require('web.FormController');
                        FormController.include({
                            async _onCloseWindow() {
                                const record = this.model.get(this.handle);
                                if (record.data.target_field) {
                                    this.displayNotification({
                                        type: 'warning',
                                        message: '目标字段不为空,无法关闭向导!',
                                    });
                                    return false;
                                }
                                return this._super(...arguments);
                            },
                        });
                    });
                </t>
            </form>
        </field>
    </record>
</odoo>

注意事项

  • 将代码中的your_module替换为你的模块名称,target_field替换为实际需要检查的字段名
  • 可根据需求调整判断条件,比如多字段组合校验、字段值匹配特定规则等
  • 提示信息可根据场景修改,也可使用web.dialogs弹出确认框替代通知

内容的提问来源于stack exchange,提问作者Wald Sin

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.17 18:01:18