Sherif
January 5, 2020, 12:39pm
1
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
sanjay
January 5, 2020, 3:49pm
4
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”);
},
Sherif
January 6, 2020, 6:28am
5
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
sanjay
January 6, 2020, 6:58am
6
Please share your complete code where you want to implement this functionality.
Sherif
January 6, 2020, 7:44am
7
// 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();
}
});
})();
sanjay
January 6, 2020, 9:55am
8
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
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:
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