How I Make Field In table ( Is Mandatory Field ) Depends On field == (Accessories)

Mandatory field == Employee
Depends On field (category) == ‘Accessories’

Custom Script

// Require employee if category is ‘Accessories’
(function() {
var requireEmployee = function() {
cur_frm.toggle_reqd(
‘employee’,
cur_frm.doc.category === ‘Accessories’’
);
};

frappe.ui.form.on(‘Customer’, {
refresh: function(frm) {
requireEmployee();
},
‘item’: function(frm) {
requireEmployee();
}
});
})();

not working

1 Like

Please try below:

frappe.ui.form.on(‘Customer’, {
setup: function(frm) {
frm.set_df_property(“employee”, “reqd”, (frm.doc.category === ‘Accessories’’));
},
})

Not Working

all Fields In Table Item

Try below (on change of category make employee field mandatory or non-mandatory):

category: function(frm) {
frm.get_docfield(“items”, “employee”).reqd = (frm.doc.category === “Accessories”);
},

not working

Frappe Framework

Close

Open Source Applications for the Web

Website: https://frappe.io

Source: Frappe · GitHub

Installed Apps

ERPNext: v7.1.11

Frappe Framework: v7.1.13

Management Reports: v0.0.1

Please share your complete code where you want to implement this functionality.

// Require employee if category item is ‘Accessories’
(function() {
var requireEmployee = function() {
cur_frm.toggle_reqd(
‘employee’,
cur_frm.doc.category_item === ‘Accessories’
);
};

frappe.ui.form.on(‘Material Request’, {
refresh: function(frm) {
requireEmployee();
},
‘item’: function(frm) {
requireEmployee();
}
});
})();

I am assuming both item_category and employee fields are in item table.

You can change df property to make it mandatory but not differently for each row as system will change df property of field in grid not grid-row (check below):

frappe.ui.form.on('Material Request Item', {
	category_item: function(frm, cdt, cdn) {
		const row = locals[cdt][cdn];
		var current_row = frm.fields_dict["items"].grid.grid_rows_by_docname[row.name];
		current_row.toggle_reqd("employee", (row.category_item === "Accessories"));
	},

});

However, you can use validate to check value of item field based on required condition as mentioned below:

cur_frm.cscript.validate = function(doc, cdt, cdn) {
	if(doc.with_items){
		$.each(doc["items"] || [], function(i, item) {
			if (item.category_item === "Accessories" && !item.employee){
				frappe.msgprint(__("Row#{0} Please enter employee.", [item.idx]));
				frappe.validated = false;
			}
		});
	}

}
1 Like

thanks a lot
Its Working

Hi Question,

What if I made a checkbox to disable the mandatory of a custom field for example is the project?

I made the below custom script:

frappe.ui.form.on(‘Sales Order’, ‘no_project’, function(frm) {
if(frm.doc.no_project == 1) {
frm.set_df_property(‘project’, ‘reqd’, 0);
} else {
frm.set_df_property(‘project’, ‘reqd’, 1) ;
}
});

The script works well. However, once I try to save the Sales order, I got this error: :frowning_face:

I also have custom scripts with the same function but instead, I use based on the value of a field. And I got the same error as the above.

1 Like