Child Table Validation - Date

Hi, I have a parent doctype called “Incident Report”. Within it is a child table called “Witness”. Within witness is a field called “Date of Birth”.

Now I want to add a validation so that the user cannot select a future date in “Date of birth”. My current code is not working, any help is appreciated. Here’s my code:

dt17” is the fieldname of the child table.

frappe.ui.form.on("Witness", "validate", function(frm, cdt, cdn) { 
 	var dt17 = frappe.get_doc(cdt,cdn);
              if (frm.doc.date_of_birth){
                 if (frm.doc.date_of_birth > frappe.datetime.get_today()){
                    frappe.msgprint(__("Enter a valid Date of Birth"));
                    frappe.validated = false;
                 }
             }
 });

Note: I have created double If conditions because before checking for the validation I wish to check if data is filled in the Date of Birth field or not. If i remove the parent If condition, it will try to validate a NULL field and that would be a problem.

1 Like

You can have both the conditions in a common if statement with an AND operator. Also, you mentioned that the field Date of birth is in the child table Witness. So you should get its value from the child table row. frm.doc will not have that field.

frappe.ui.form.on("Witness", "validate", function (frm, cdt, cdn) {
	var dt17 = frappe.get_doc(cdt, cdn);
	if (dt17.date_of_birth && dt17.date_of_birth > frappe.datetime.get_today()) {
		frappe.msgprint(__("Enter a valid Date of Birth"));
		frappe.validated = false;
	}
});

Also, ensure that the date for birth is a proper date field else the comparison won’t work.

I tried it, it still saves even if the date is in the future and yes the date of birth field is a date field :frowning:

Can you share the value entered in the date of birth field?

Hi @Vesper_Solutions

It’s a year ago, but I’m working on a related validation problem on a child table :

and thought I’ll add what I know and maybe someone else can help me with my problem.

If I’m not mistaken, child tables do not have a validate event, hence your code will not trigger. Move the validate event to the parent ‘Incident Report’.