Passa al contenuto
Menu
È necessario essere registrati per interagire con la community.
La domanda è stata contrassegnata
2 Risposte
627 Visualizzazioni

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.

Avatar
Abbandona
Risposta migliore


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.



Avatar
Abbandona
Risposta migliore

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

Avatar
Abbandona
Post correlati Risposte Visualizzazioni Attività
0
gen 25
1608
0
set 23
3534
2
ago 23
12041
0
ago 23
194
1
mag 23
2737