frm.set_read_only() This JavaScript snippet is used in Frappe Client Scripts to prevent users from editing certain records and to clearly explain why the restriction exists. This pattern is commonly applied to system-controlled or root records such as root departments, companies, or core DocTypes “TO PREVENT ANY EDIT ON IT”.
frmobject represents the currently opened form. By using its APIs, we can control the form’s behaviors.
Making the Form Read-Only
The method frm.set_read_only() turns the entire form into a read-only state. All input fields become non-editable and the user can only view the data. This is useful when a document should not be modified directly, but still needs to be visible.
BTW frm.set_df_property("fieldname", "read_only", 1) turns specific field in the form into a read-only state,
but frm.set_read_only() turns the entire form into a read-only state.
frm.set_read_only()/ per formfrm.set_df_property("fieldname", "read_only", 1)/ per field
Showing a Message
The frm.dashboard.add_comment() and frm.set_intro() function displays a short explanation just below the form title. To informs the user that the record cannot be edited, reducing confusion and support questions.
Code Snippet
refresh: (frm)=>{
if (your condition){
frm.set_read_only();
frm.dashboard.add_comment(__("This document cannot be edited."),"blue",true);
//or
frm.set_intro(__("This document cannot be edited."), "red");
}
}