I have created a client script for the Attendance Doctype with the rule: “Cannot save attendance for a past month unless the date is the current month 1st or 2nd.” However, when using the Mark Attendance button to submit attendance for a past month’s day, the attendance is still accepted. How can I control the button to enforce this rule?
frappe.ui.form.on(‘Attendance’, {
validate: function(frm) {
const attendance_date = frm.doc.attendance_date;
const today_date = frappe.datetime.now_date();
const attendanceMonth = parseInt(attendance_date.split(“-”)[1]);
const todayMonth = parseInt(today_date.split(“-”)[1]);
const attendanceDay = parseInt(today_date.split(“-”)[2]);
// Check if attendance month is before the current month and date is not the 1st or 2nd
if (attendanceMonth < todayMonth && ![1, 2].includes(today_date)) {
frappe.msgprint(__(‘Cannot save attendance for a past month unless the date is the 1st or 2nd.’));
frappe.validated = false; // Prevent form submission
}
}
});
You can handle this more effectively on the server side. Please review the syntax carefully and implement it based on the specific scenario.
attendance_date = frappe.utils.getdate(doc.attendance_date)
today_date = frappe.utils.getdate(frappe.utils.nowdate())
attendance_month = attendance_date.month
today_month = today_date.month
today_day = today_date.day
if attendance_month < today_month and today_day not in [1, 2]:
frappe.throw(
"Cannot save attendance for a past month unless the date is the 1st or 2nd."
)