Client Script validate field Allow on Submit before update on submitted form

Hi.

I have some custom fields in the Sales Invoice doctype with “Allow on Submit” checked. I need to validate that these fields are not empty, and if they are, I need to prevent the update to happen.

This is the Client Script I have tried but the events before_save or validate do not work

after_save does work when the form is submitted, but then I can’t prevent it from updating

frappe.ui.form.on('Sales Invoice', {    
    before_save: function(frm) { 
        if (frm.doc.docstatus === 1) {
            if ((!frm.doc["orden_laboratorio_óptico"]) || (!frm.doc.laboratorio) || (!frm.doc["fecha_orden_laboratorio_óptico"])) {
                frappe.validated = false;
                frappe.msgprint(__("Some fields are missing."));
            } else {
                frm.doc.seguimiento_de_orden_de_laboratorio_creada = "1";
                frm.refresh_field('seguimiento_de_orden_de_laboratorio_creada'); 
                frappe.msgprint(__("Seguimiento Created succesfully."));
            }
        }
    }  
});

Any help is greatly appreciated

Hi @CostaRica,

This can be better handled on the server script side. please check it.

script sample:

if not doc.po_no:
    frappe.throw("PO Number missing")
else:
    frappe.msgprint("Value updated")

Thanks, @NCP for taking your valuable time to answer. It is working great

For those who read this, remember to go to frappe-bench directory and run the following command in order to “activate” the script

bench restart

From chatgpt:

Running bench restart will not cause users to lose any data. When you run bench restart, it restarts the Frappe server, which essentially stops and then restarts the Frappe application.

During this process:

Existing user sessions may be interrupted temporarily, but users will be able to log back in once the server restarts.

Any ongoing operations, such as form submissions or background tasks, may be temporarily halted, but they will resume once the server restarts.

Data stored in the database remains intact. There is no data loss associated with restarting the server.

However, it's always a good practice to ensure that there are no critical operations or user activities occurring at the time of the restart, as they may be interrupted.