跳至内容
菜单
此问题已终结
2 回复
434 查看

I want to add validation for the email field in the Contacts app, however, I don't know what libraries should I import in my JS file. Also, how to edit the field validation using OWL.

形象
丢弃
最佳答案


Hello Khaled,

Here’s an example of how you can add a simple validation for the email field in the Contacts app using OWL. You don’t need any extra libraries; OWL and the web client utilities are already available in Odoo.


/** @odoo-module **/

import { CharField } from "@web/views/fields/char/char_field";

import { patch } from "@web/core/utils/patch";

patch(CharField.prototype, "email_validation_patch", {

    async onInput(ev) {

        await super.onInput(ev);

        if (this.props.name === "email") {

            const value = ev.target.value || "";

            const emailPattern = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;

            const isValid = emailPattern.test(value);

            if (!isValid && value) {

                this.notification.add("Please enter a valid email address.", { type: "warning" });

                ev.target.classList.add("o_invalid_field");

            } else {

                ev.target.classList.remove("o_invalid_field");

            }

        }

    },

});


Add this file under your custom module path:

/static/src/js/email_validation.js

Then include it in your manifest file:

'assets': {

    'web.assets_backend': [

        '/your_module/static/src/js/email_validation.js',

    ],

},


After updating and reloading, the email field will show a warning message when the format is invalid.



形象
丢弃
最佳答案

Hi,


The error occurs because your pos_data_loader.js is importing @point_of_sale/app/store/pos_global_state, which exists only in Odoo 19, not Odoo 18. In Odoo 18, the equivalent is point_of_sale.models or PosModel, and your code should patch its prototype instead. Additionally, your custom JS must be included in the point_of_sale.assets bundle in your module manifest. Using Odoo 19 import paths in Odoo 18 causes unmet dependency errors, so adapting the imports and ensuring proper asset inclusion will resolve the issue.


Try the following code,


/** @odoo-module **/

import { patch } from "web.utils";

import PosModel from "point_of_sale.models";  // <-- Correct path for v18


console.log("PSMS: Extending POS data models for Odoo 18");


patch(PosModel.prototype, "psms_pos_data_loader", {

    async _load_data() {

        console.log("PSMS: _load_data running...");


        await super._load_data();


        const orm = this.env.services.orm;

        if (!orm) return;


        const configs = await orm.searchRead("pos.config", [], [

            "id", "name", "display_stock", "nozzle_ids", "pump_ids", "tanks_ids"

        ]);


        const currentConfigId = this.config_id;

        const currentConfig = configs.find(c => c.id === currentConfigId);


        if (!currentConfig) return;


        this.display_stock = currentConfig.display_stock;

        this.nozzle_ids = currentConfig.nozzle_ids || [];

        this.pump_ids = currentConfig.pump_ids || [];

        this.tanks_ids = currentConfig.tanks_ids || [];


        console.log(" POS CONFIG", this);


        // Load nozzle data if any

        if (this.nozzle_ids.length) {

            const nozzles = await orm.searchRead("petrol.nozzle", [["id", "in", this.nozzle_ids]], [

                "id", "name", "pump_id", "tank_id", "product_id"

            ]);

            this.db.nozzle_by_id = {};

            for (const nozzle of nozzles) this.db.nozzle_by_id[nozzle.id] = nozzle;

            console.log(` PSMS: Loaded ${nozzles.length} nozzles for this POS`);

        }

    }

});


Hope it helps

形象
丢弃
相关帖文 回复 查看 活动
0
1月 25
1494
0
9月 23
3416
2
8月 23
11669
0
8月 23
194
1
5月 23
2656