Validation for File Types in Web Forms

The following code works fine in a doctype but not in a web form. This is because the attached_to_doctype and attached_to_field values are not populated when uploading through the web form.

def validate_file_type(doc, method):
    if doc.attached_to_doctype == 'Test Form':
        if doc.attached_to_field in ["photograph", "signature"]:
            if not doc.file_name.lower().endswith(".jpg") or not doc.file_name.lower().endswith(".jpeg"):
                frappe.throw("Only <b>JPG/JPEG</b> file Allowed")
            if not doc.file_name.lower().endswith(".pdf"):
                frappe.throw("Only <b>PDF</b> file Allowed")


  • For fields like photograph and signature, the uploaded file should always be in JPG/JPEG format.
  • For all other attach fields, only PDF files should be allowed.

How can I achieve this validation for my web form where attached_to_doctype and attached_to_field are not available during file upload?