From f4cd8bf6b30e0a4abc0a9d71c2dd09644364dc25 Mon Sep 17 00:00:00 2001 From: Shariq Ansari Date: Thu, 4 Jan 2024 21:26:18 +0530 Subject: [PATCH] fix: show asterisk for mandatory fields and also validate before saving --- crm/api/doc.py | 11 ++++++++--- crm/fcrm/doctype/crm_deal/api.py | 2 +- frontend/src/components/SectionFields.vue | 1 + frontend/src/pages/Deal.vue | 16 ++++++++++++++++ 4 files changed, 26 insertions(+), 4 deletions(-) diff --git a/crm/api/doc.py b/crm/api/doc.py index a704bb94..9795246c 100644 --- a/crm/api/doc.py +++ b/crm/api/doc.py @@ -188,11 +188,16 @@ def get_doctype_fields(doctype): else: section_fields.append(get_field_obj(field)) - all_fields = [] + section_fields = [] for section in sections: - all_fields.append(sections[section]) + section_fields.append(sections[section]) - return all_fields + fields = [field for field in fields if field.fieldtype not in "Tab Break"] + fields_meta = {} + for field in fields: + fields_meta[field.fieldname] = field + + return section_fields, fields_meta def get_field_obj(field): diff --git a/crm/fcrm/doctype/crm_deal/api.py b/crm/fcrm/doctype/crm_deal/api.py index e37ca74d..2ab69e14 100644 --- a/crm/fcrm/doctype/crm_deal/api.py +++ b/crm/fcrm/doctype/crm_deal/api.py @@ -27,7 +27,7 @@ def get_deal(name): fields=["contact", "is_primary"], ) - deal["doctype_fields"] = get_doctype_fields("CRM Deal") + deal["doctype_fields"], deal["all_fields"] = get_doctype_fields("CRM Deal") deal["doctype"] = "CRM Deal" deal["_form_script"] = get_form_script('CRM Deal') return deal diff --git a/frontend/src/components/SectionFields.vue b/frontend/src/components/SectionFields.vue index 6932b3aa..c6d504c2 100644 --- a/frontend/src/components/SectionFields.vue +++ b/frontend/src/components/SectionFields.vue @@ -8,6 +8,7 @@ >
{{ field.label }} + {{ field.reqd ? ' *' : '' }}
{ function updateDeal(fieldname, value, callback) { value = Array.isArray(fieldname) ? '' : value + if (validateRequired(fieldname, value)) return + createResource({ url: 'frappe.client.set_value', params: { @@ -386,6 +388,20 @@ function updateDeal(fieldname, value, callback) { }) } +function validateRequired(fieldname, value) { + let meta = deal.data.all_fields || {} + if (meta[fieldname]?.reqd && !value) { + createToast({ + title: 'Error Updating Deal', + text: `${meta[fieldname].label} is a required field`, + icon: 'x', + iconClasses: 'text-red-600', + }) + return true + } + return false +} + const breadcrumbs = computed(() => { let items = [{ label: 'Deals', route: { name: 'Deals' } }] items.push({