Mandatory field - only restrict "Submit" if missing, allow "Save"

I want to restrict Purchase Invoices to be submitted without a Supplier Inoivce No. (bill_no). However I’d want this restriction only affect Submit , not affect the Save action. So we can create draft Purchase Invoice even if we have no bill_no information available yet.

I have checked the “Supplier Invoice No.” field as Mandatory via Customize Form, but this restricts saving Invoices also.

Is there a way to only apply the restriction to the Submit, but not the Save action?

you have to writhe a custom script for that and apply it on event “before submit”

o
I was afraid that would be the answer :expressionless: . Anyway, maybe a good incentive to start digging into that aspect then.

frappe.ui.form.on(“Purchase Invoice”, “before_submit”, function(frm, cdt, cdn) {
if(!frm.doc.bill_no){
frappe.throw("You Cannot Submit Invoice without Bill No. ");
frappe.validated = false; }
})

it’s tested and working fine.

i hope this will make your day.

let me know if you need help to paste this script.

fantastic! thanks a mil.
I"ll try it later today. I guess I know where to apply it.

out of curiosity, what kind of language is that written in?

it’s javascript.

throws this error at me as soon I say “New Purchase Invoice”

SyntaxError: Invalid or unexpected token
    at init.setup (http://ip.address/assets/js/form.min.js?ver=1553106235.0:1:60054)
    at _f.Frm.setup (http://ip.address/assets/js/form.min.js?ver=1553106235.0:1:13114)
    at _f.Frm.refresh (http://ip.address/assets/js/form.min.js?ver=1553106235.0:1:19032)
    at frappe.views.FormFactory.load (http://ip.address/assets/js/form.min.js?ver=1553106235.0:1:11600)
    at http://ip.address/assets/js/form.min.js?ver=1553106235.0:1:11451
    at Object.callback (http://ip.address/assets/js/desk.min.js?ver=1553106235.0:1:115034)
    at Object.success [as success_callback] (http://10.19.225.137/assets/js/desk.min.js?ver=1553106235.0:1:49056)
    at 200 (http://ip.address/assets/js/desk.min.js?ver=1553106235.0:1:49424)
    at Object. (http://ip.address/assets/js/desk.min.js?ver=1553106235.0:1:51916)
    at i (http://ip.address/assets/frappe/js/lib/jquery/jquery.min.js:2:27151)

@adnan Please don’t feel obliged to debug this for me (even though you are most welcome to). I put this here mainly as a reference so I can come back to this at some point

HI,
Strange it dosent work with you. the error might be because of (") Double Quotes.

jut try to remove the double quotes and put them manually.

i’m confident it’ll work as i tested it before i post it here.

good guess!

frappe.ui.form.on("Purchase Invoice", "before_submit", function(frm, cdt, cdn) {
if(!frm.doc.bill_no){
frappe.throw("You Cannot Submit Invoice without Supplier's Invoice Number. ");
frappe.validated = false; }
})

(with ‘good’ double quotes) works like a charm. That being said … the “Mandatory” in the bill_no field needs to be not checked now. The script does the job (blocking “Submit”) by itself.


@adnan: I think this may have happened because you put the code as regular text in here.
3 back ticks (`) above and 2 back ticks below such a code snippet formats the text in between as block of code here in the discourse forun, which may have prevented the encoding properties of the double quotes

Hi @adnan

Thanks a lot for this… I was just looking for a solution to the same issue. What if there are several such fields? What’s the correct syntax?

Cheers

you are Welcome, glad this helped you.

if you want to mention each missing field in msg. then keep repeating the above block with new field name.

otherwise we can mention all the fields in one row and can generate one generic messag for user.

Regards,
Adnan

What’s the correct syntax… separted with comma? An example would help a lot

Thanks!

if((!frm.doc.bill_no) && (!frm.doc.bill_date) && (!frm.doc.xyz)) {
frappe.throw("You Cannot Submit Invoice without these fields empty, . ");
frappe.validated = false; }

Thanks a great deal @adnan

I really think this should be a field option so that everyone can easily use it. It’s an essential feature especially for managing longer forms; users should be able to save forms as they go along and only get an error message when they try to Submit. So basically, there should be 2 options for ‘Mandatory’ in every field:

  1. Mandatory (Before Save)
  2. Mandatory (Before Submit)

I’ve created a Github issue for this Mandatory Before Submit is required in field options · Issue #7316 · frappe/frappe · GitHub

Thanks again for your help @adnan …much appreciated

Yes Wale,

this should be the part of frappe core. can be achieved.

you are welcome.