How to use frappe.msgprint to show the exact Row number with error

Hi! How can frappe.msgprint return the exact row number that is triggering the error message. Help please!

Here is my code:

frappe.ui.form.on(‘Rental Billing Details’, ‘hours_of_operation’, function(frm, cdt, cdn) {
$.each(frm.doc.rental_billing_details || [], function(i, d) {

            if(d.hours_of_operation < 0){
                frappe.msgprint({
                    title: __('Error'),
                    indicator: 'red',
                    message: __('Row {0}: Hours of Operation cannot be Negative')
                });
                validated = false;
            }
        });
    });

Herein the error message:

Thank you!

use something like this

cur_frm.cscript.validate = function(doc, cdt, cdn) {
    if (doc.table_fieldname) {
        $.each(doc["table_fieldname"] || [], function(i, d) {
            if (!d.image) {
                frappe.msgprint(__("Row#{0} Please Enter Design Name and upload File.", [d.idx]));
                frappe.validated = false;
            }
        });
    }
};
1 Like

Hi @mohitchechani ! Thank you so much for your quick response. Will this script prevent form from saving when this error still persists?

Hi @mehmehly,

Please try it.

frappe.ui.form.on('Rental Billing Details', 'hours_of_operation', function(frm, cdt, cdn) {
    var validated = true;
    $.each(frm.doc.rental_billing_details || [], function(i, d) {
        if (d.hours_of_operation < 0) {
            frappe.msgprint({
                title: __('Error'),
                indicator: 'red',
                message: __('Row {0}: Hours of Operation cannot be Negative', [i + 1])
            });
            validated = false;
        }
    });
    if (!validated) {
        frappe.validated = false; // Prevent saving if there are errors
    }
});

Then reload and check it.

Thank You!

1 Like

I believe you should use ‘frappe.throw’ instead of ‘frappe.msgprint’ to prevent saving

I will try this, thank you!

This script worked for me:

if(d.hours_of_operation < 0){
frappe.msgprint({
title: __(‘Error’),
indicator: ‘red’,
message: __(‘Row {0}: Hours of Operation cannot be Negative’, [d.idx])
});
validated = false;
}

and displayed error message below:

image

Thank you! @mohitchechani and @NCP