How to properly fail validation?

I’m copying this example - Frappe Cloud - but my “throw” generates a JS error.

frappe.ui.form.on("Project", "validate", function(frm) {
    if ((frm.doc.camera_live==null || !frm.doc.camera_live || frm.doc.camera_live=="") && frm.doc.status=="Live") {
        msgprint(__("You must set a live date before altering camera status."));
        throw "Invalid data";
    }
})

The validation triggers properly but the throw is uncaught, makes a JS error, and then I have to reload my page. On the plus side, the document does NOT get saved with the bad data.

If I change the throw to a “return false”, there is no JS error created but the document is still saved with the invalid data – the validation is not aborted.

How do I properly throw/catch the invalid data and stop execution of the document being saved?

Set the global validated property to false.

validated = false;

the exception should not have been caught though

Hooray – it works! Thanks!

if ((frm.doc.camera_live==null || !frm.doc.camera_live || frm.doc.camera_live=="") && frm.doc.status=="Live") {
    msgprint(__("You must set a live date before altering camera status."));
    validated=false;
    return false;
}
1 Like