How to set a field to be Mandatory using Client Script?

Hello,

Based on the Item selected by user I want to make a few fields Mandatory or Optional.

One way out is to use the validate but instead of that want to offload the Mandatory part on Framework.

How to set a field to be Mandatory using Client Script?

TIA

Yogi Yang

Hello,

I am using following code:

item_code: function(frm, cdt, cdn) {
		var d = locals[cdt][cdn];
        frappe.db.get_value('Item', {name: d.item_code}, 'is_wire', (r) => {
            console.log(r.is_wire);
            frappe.msgprint(r.is_wire);
            if(r.is_wire > 0){
                frm.toggle_reqd('coil_id', true);
                frm.toggle_reqd('supplier_code', true);
                frm.toggle_reqd('material_specs', true);
                frm.toggle_reqd('coil_inward_date', true);
                frm.toggle_reqd('coil_heat_num', true);
                frm.toggle_reqd('coil_batch_no', true);
                frm.toggle_reqd('coil_qty', true);
                frm.toggle_reqd('coil_wt', true);
                frm.toggle_reqd('wire_size', true);

                //Show the Fields
                frm.toggle_display(['wire_coil_details'], true);
                frm.toggle_display(['coil_id'], true);
                frm.toggle_display(['supplier_code'], true);
                frm.toggle_display(['material_specs'], true);
                frm.toggle_display(['coil_inward_date'], true);
                frm.toggle_display(['column_break_12'], true);
                frm.toggle_display(['coil_heat_num'], true);
                frm.toggle_display(['coil_batch_no'], true);
                frm.toggle_display(['coil_qty'], true);
                frm.toggle_display(['coil_wt'], true);
                frm.toggle_display(['wire_size'], true);
            }
            else{
                frm.toggle_reqd('coil_id', false);
                frm.toggle_reqd('supplier_code', false);
                frm.toggle_reqd('material_specs', false);
                frm.toggle_reqd('coil_inward_date', false);
                frm.toggle_reqd('coil_heat_num', false);
                frm.toggle_reqd('coil_batch_no', false);
                frm.toggle_reqd('coil_qty', false);
                frm.toggle_reqd('coil_wt', false);
                frm.toggle_reqd('wire_size', false);

                //Hide the Fields
                frm.toggle_display(['wire_coil_details'], false);
                frm.toggle_display(['coil_id'], false);
                frm.toggle_display(['supplier_code'], false);
                frm.toggle_display(['material_specs'], false);
                frm.toggle_display(['coil_inward_date'], false);
                frm.toggle_display(['column_break_12'], false);
                frm.toggle_display(['coil_heat_num'], false);
                frm.toggle_display(['coil_batch_no'], false);
                frm.toggle_display(['coil_qty'], false);
                frm.toggle_display(['coil_wt'], false);
                frm.toggle_display(['wire_size'], false);
            }
		});
	}

But it is not working.

TIA

Yogi Yang

hello, is this for child table or main doc, if you trying to set child table field i think you can use the “d” variable but not where your code run

Hello,

It is a child table.

TIA

Yogi Yang

It did not work for me for child table. Am able to set values but not able to toggle the mandatory.

Try using a for loop using form values instead of pulling db.values.

@Vesper_Solutions, I have tried with simple single field too. It failed to give desired results. Below is the code which I tried to replicate the scenario.

frappe.ui.form.on('Service Plan', {
	refresh(frm) {
		// your code here
		console.log("Here");
	}
});

// SERVICE PRIORITY MAPPING
frappe.ui.form.on('Service Priority Mapping',{
    priority: function(frm,cdt,cdn){
        // test this up
        //frm.toggle_reqd('duration', true);
        //frm.refresh_field('duration');
        
        //frm.set_df_property('alternate', 'reqd', 1);
        frm.set_df_property("duration", "reqd", true);
        frappe.model.set_value(cdt, cdn, "alternate", "saved");
        let row = locals[cdt][cdn];
        console.log("In Parent Here");
        //row.alternate = "saved" ;
        refresh_field("alternate");
    }
});

Try using a for each loop.

$.each("child_table_variable_name", function (index, row) {

                  //validation check goes here
   });

@Vesper_Solutions , apology for the confusion. We are able to get field values but not able to make it mandatory conditionally. Am able to set the field value too.

Try this
frm.set_df_property("variable_name", "reqd", 1);

@Vesper_Solutions,

The solution you suggested is not working for me.

TIA

Yogi Yang

this didn’t work for me too. am able to set value by this but not the required field.

1 Like

Hello,

I have tried using frm.toggle_reqd('coil_id', true); to make a field mandatory in Child Table but it is not working. Is there any other way to set a field to be Mandatory?

TIA

Yogi Yang

Hello,

Finally I managed to get this working!

This code worked for me.

frm.fields_dict["items"].grid.toggle_reqd('coil_id', true);

TIA

Yogi Yang

But if I select an Item whose is_wire is false then select an Item whose is_wire is true it forces me to enter values in the fields that get set as Mandatory.

Here is a screen shot.
Screenshot from 2022-09-24 17-46-46
Here the First item is Lub Oil (whose is_wire is false) and the Second item is Wire (whose is_wire is true).

How to resolve this?

TIA

Yogi Yang

Why so complex solutions when it can be easily handled from customize form or custom field.

Check out mandatory_depends_on as a part of custom field.

Check its application here.

2 Likes