Making Field Read Only On Select Option

Hello,
I am trying to make a field read only on the select option as if yes and no. On Yes I want to make all fields read only without saving. How can i do that? Any suggestions?

frappe.ui.form.on("[your doctype]", "[your select field]", function(frm) {
    // if field is yes:
    $.each(frm.meta.fields, function(index, field)) {
	    frm.set_df_property(field.fieldname, "read_only", 1);
    });
}
4 Likes

Can you suggest an example for the code you’ve mentioned, specifying all the values?
Also, If there are three values in select then what should be done? @snv

Why don’t you try experimenting instead? Because that’s how you learn.

This is basic JS. Add another if statement. Why would you have three values though?

1 Like

I have done experimenting. Didn’t got the appropriate result. so thought of asking an example.

frappe.ui.form.on(“Saleable Unit”, “status”, function(frm) {
// if field is yes:
$.each(frm.meta.fields, function(index, status)) {
frm.set_df_property(status.sold, “read_only”, 1);
});
}

The status is having more fields like unsold, sold, hold. That’s the reason I put up that question.
I am a newbie. I googled a solution for this.
Didn’t workout so.
If you can contribute, it would be of great help. @snv
Thanks.

frappe.ui.form.on(“Saleable Unit”, “status”, function(frm) {
if (frm.doc.status === 'Sold') {
    $.each(frm.meta.fields, function(index, field)) {
        if (field.fieldname != 'status') {
        frm.set_df_property(field.fieldname, “read_only”, 1);
     }
    });
    frm.refresh();
}
});

Protip: Go through the tutorials here before continuing.

2 Likes

Hi bro,
I am exploring frappe. I created a webform and write functionality in the webform.js if every field gets value i call that function. It works after goes to edit page. I want to set the webform fields read only in edit page. Is it any option to do that.

If user went to edit page of webform he changes any field means the function again called. It’s not needed i want to update only form_name in webform. That’s why i am trying to set fields read only in edit page. Is it possible?