Prevent users to submit Purchase Orders if there are no attachments

How can I check for the attachment of a doctype? if it is empty, I want to prevent the users from submitting the document.

thanks for you code. I tried this code, but it didn’t work. What did I do wrong?

frappe.ui.form.on('Purchase Order', {
  validate: function(frm) {
    if (frm.attachments.get_attachments().length === 0) {
      frappe.throw(_("Please attach at least one file to the Purchase Order."));

I tried your code and it didn’t work.

I’m sorry couldn’t test it earlier.
Try creating a server script before submit;

attachments = frappe.get_all("File", filters={"attached_to_doctype": doc.doctype, "attached_to_name":})
if not attachments:
  frappe.throw("Please attach a file before submitting the document.")

Tested and works as of V14.