How can I add validator to Purchase Order(or other system doctypes)?

I want to add a validate function to Purchase Order, but I don’t want to override current validate method, how to append a custom validator to a doctype without modifying original codes?

If I create a hook to validate in a custom app, would it override origin or append?

1 Like

@nnylyj
You can add custom script for this
Example:

frappe.ui.form.on("Purchase Order", "validate", function(frm) {
    if (frm.doc.from_date < get_today()) {
        msgprint("You can not select past date in From Date");
        validated = false;
    }
});

Here
http://frappe.github.io/erpnext/user/manual/en/customize-erpnext/custom-scripts/custom-script-examples/

1 Like

thanks but I need to validate in backend, because I create po by API

1 Like

@nnylyj ok, then you can use hooks to call validate method

call your custom python function on validate event.
add this in hooks.py under doc_events

write your python function, you have access to all field value using self.field_name.
To invalidate you can write frappe.throw(“message”)

thanks, and a new problem is will the hook override previous validator or just append to previous validators?

@nnylyj

It’s append to previous one.
Both functionality will run.

1 Like

Thanks for the answer! :smile:

UPDATE:
I added a validate hook to a doctype, but nothing happened after clicking Save button.
WHEN the validate hook will be invoked?