nnylyj
February 22, 2016, 11:03am
1
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
nnylyj
February 22, 2016, 11:51am
3
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
app_color = "#589494"
app_email = "info@systematrix.co.in"
app_url = "https://github.com/Systematrix/SF_Custom_Changes"
app_version = "12.6.0"
fixtures = ["Custom Field",
"Property Setter",
"Custom Script",
"Print Format",
"Report"]
doc_events = {
"Purchase Invoice": {
"validate": "sf_custom_changes.sf_acc.purchase.validate_bill_no"
}
}
# Includes in <head>
# ------------------
# include js, css files in header of desk.html
# app_include_css = "/assets/sf_custom_changes/css/sf_custom_changes.css"
write your python function, you have access to all field value using self.field_name.
To invalidate you can write frappe.throw(“message”)
@frappe.whitelist(allow_guest=True)
def validate_mandatory(self):
# validate transaction date v/s delivery date
if self.delivery_date:
if getdate(self.bill_date) > getdate(self.posting_date):
frappe.throw(_("Expected Delivery Date cannot be before Sales Order Date"))
@frappe.whitelist(allow_guest=True)
def validate_bill_no(self, method):
if self.bill_no:
# validate bill no is unique
bill_list = frappe.db.sql("""select name from `tabPurchase Invoice` where bill_no=%s and docstatus=1 and is_recurring='0'""", self.bill_no)
if len(bill_list) > 0:
items = [e[0] for e in bill_list if e[0]!=self.name]
frappe.throw(_("Supplier Invoice Number must be unique. Current Supplier Invoice Number already exists for {0}").format(comma_and(items)))
if self.bill_date:
if getdate(self.bill_date) > getdate(self.posting_date):
frappe.throw(_("Supplier Invoice Date cannot be after Purchase Invoice Date"))
nnylyj
February 23, 2016, 1:03am
5
thanks, and a new problem is will the hook override previous validator or just append to previous validators?
Sangram
February 23, 2016, 5:30am
6
@nnylyj
It’s append to previous one.
Both functionality will run.
1 Like
nnylyj
February 23, 2016, 6:12am
7
Thanks for the answer!
UPDATE:
I added a validate hook to a doctype, but nothing happened after clicking Save button.
WHEN the validate
hook will be invoked?