How to make a field mandatory in dialog

I have a dialog in which I have placed 3 Check box fields and 1 data field.

If Check box 1 or 2 is checked I want to make the Data field mandatory.

If Check box 1 and 2 are not checked I want to make the Data file optional.

How to do this?

Hi @YogiYang

You can simply add it like this in the data field that you want to make mandatory eval:doc.{check_box_fieldname} == 1, So whenever it gets selected the data field will get change to required field

Hello @keerthana_2001,

Thanks for taking the time to reply.

Probably you did not understand my requirements.

I do not want to set mandatory in DocType. I want to achieve this in a Dialog that is created in Client Script using JS.

What you have suggested is working in DocType but not working in Dialog.

Hi @YogiYang:

Set this property

"reqd": 1

See this example:

frappe.ui.form.on('Request for Quotation', {
	refresh(frm) {
	    if (frm.doc.docstatus === 1) {
			frm.add_custom_button(__('Supplier Quotation'),
				function(){ frm.trigger("make_supplier_quotation") }, __("Create"));
	    }
	},
	make_supplier_quotation:function(frm){
	    var doc = frm.doc;
		var dialog = new frappe.ui.Dialog({
			title: __("Create Supplier Quotation"),
			fields: [
				{	
				    "fieldtype": "Link",
					"label": __("Supplier"),
					"fieldname": "supplier",
					"options": 'Supplier',
					"reqd": 1,
					get_query: () => {
						return {
							filters: [
								["Supplier", "name", "in", frm.doc.suppliers.map((row) => {return row.supplier;})]
							]
						}
					}
				}
			],
			primary_action_label: __("Create"),
			primary_action: (args) => {
				if(!args) return;
				dialog.hide();

				return frappe.call({
					type: "GET",
					method: "erpnext.buying.doctype.request_for_quotation.request_for_quotation.make_supplier_quotation_from_rfq",
					args: {
						"source_name": doc.name,
						"for_supplier": args.supplier
					},
					freeze: true,
					callback: function(r) {
						if(!r.exc) {
							var doc = frappe.model.sync(r.message);
							frappe.set_route("Form", r.message.doctype, r.message.name);
						}
					}
				});
			}
		});

		dialog.show()
	    
	}
})

Hope this helps

Mandatory depends on conditions will not work if your DocType have Workflow.

I want to make a field mandatory only when certain checkbox are true not otherwise. And this is in a Dialog not in DocType.

@YogiYang

Check the code posted above, use a conditional to build your fields list with or without “reqd” property.

This is running for dialog, not for doctype form

Thanks for the code sample. I checked it but what I want is to show user a Dialog.

In the dialog if the user selects Check box 1 or 2 then the Data field should become mandatory otherwise it should be optional.

I have already done something like this in a few instances.

But this time it is while the dialog is visible to the user.

I hope I have managed to explain my requirement properly.

Hi @YogiYang:

Use something like this:

var dialog = new frappe.ui.Dialog({
			title: __("Your dialog"),
			fields: [
				{	
				    "fieldtype": "Data",
					"label": __("Your field"),
					"fieldname": "yourfield",
					"reqd": 1,
                },
                {	
				    "fieldtype": "Check",
					"label": __("Enabled"),
					"fieldname": "enabled",
					"reqd": 1,
                    change: () => {
                        if (dialog.get_value("enabled")==1) {
                            dialog.set_df_property("yourfield", "reqd", 0)
                        }
                        else {
                            dialog.set_df_property("yourfield", "reqd", 1)
                        }
                    }
						
                }
            ]});

Hope this helps.

1 Like