Try ERPNext Buy Support Partners Foundation

Date Validation Script from Documentation not working

I created my own Doctype called “Staff Policies and Procedures”. I wanted to validate a date field with the fieldname “date” to limit the user to not select future dates. So i copied the date validate code from the documentation, made the required changes but the script does not work and the form saves even if I select a future date. Here is my code

frappe.ui.form.on(“Staff Policies and Procedures”, “validate”, function(frm) {
if (frm.doc.date > get_today()) {
frappe.msgprint(__(“You can not select future date in From Date”));
frappe.validated = false;
}
});

Hi @Vesper_Solutions did you already define get_today() ??
Maybe try using frappe.datetime.get_today(), it’s build in function from Frappe to get today.

Tried it, doesn’t work. Here’s the code:

frappe.ui.form.on(“Staff Policies and Procedures”, “validate”, function(frm) {
if (frm.doc.date > frappe.datetime.get_today()) {
frappe.msgprint(__(“You can not select future date in From Date”));
frappe.validated = false;
}
});

Hmm that’s weird.
Maybe try to frappe.msgprint before if
To see is the hook works or not…

The hook works, I have used it in other validation conditions for other doctypes. The issue lies elsewhere.

Hello.

You might want to look up this.

Regards,

Ivan

Mate it’s literally in the code I wrote:

frappe.ui.form.on(“Staff Policies and Procedures”, “validate”, function(frm) {
     if (frm.doc.date > frappe.datetime.get_today()) {
          frappe.msgprint(__(“You can not select future date in From Date”));
          frappe.validated = false;
     }
});

Doesn’t work

Hello,

Please check.

frappe.datetime.get_today()
frappe.datetime.get_day_diff("2016-05-05", "2016-05-03" )
frappe.datetime.add_days("2016-05-03", 2)

Regards,

Ivan


image

pay attention to the quote around validate.

Working now, thanks :smiley: