Date Validation

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

This script validates the "From Date" field in the "Task" DocType to ensure that users do not select a date that is in the past. If they attempt to do so, it displays an error message and prevents the form from being submitted. This helps maintain data integrity and ensures that all dates entered are valid and relevant.

  1. Event Binding:
    frappe.ui.form.on("Task", "validate", function(frm) {...});
    This line binds a function to the validate event of the "Task" DocType. The validate event is triggered right before the document is saved.

  2. Checking the Date:
    if (frm.doc.from_date < get_today()) {...}
    This line checks if the value of the fromdate field in the current document (frm.doc.fromdate) is less than today’s date (gettoday()). The gettoday() function returns the current date in the appropriate format.

  3. Displaying an Error Message:
    frappe.msgprint(__("You can not select past date in From Date"));
    If the condition is true (meaning the selected from_date is in the past), this line shows an error message to the user, informing them that they cannot select a past date.

  4. Preventing Form Submission:
    frappe.validated = false;
    Setting frappe.validated to false prevents the document from being saved. This is crucial as it ensures that if the validation fails, the user cannot submit the form.
Discard
Save
Was this article helpful?