From 2e70a0167a7fb8083c1666f76708800ac256ecce Mon Sep 17 00:00:00 2001 From: Faris Ansari Date: Fri, 27 Dec 2019 15:53:13 +0530 Subject: [PATCH] fix: Add validations for email and phone fields --- .../AccountingSettings/AccountingSettings.js | 5 +++- models/doctype/Party/Party.js | 10 ++++++-- models/doctype/PrintSettings/PrintSettings.js | 10 ++++++-- models/doctype/SetupWizard/SetupWizard.js | 4 +++- src/components/TwoColumnForm.vue | 2 +- src/utils.js | 23 +++++++++++++++---- 6 files changed, 43 insertions(+), 11 deletions(-) diff --git a/models/doctype/AccountingSettings/AccountingSettings.js b/models/doctype/AccountingSettings/AccountingSettings.js index 195aee44..6801e13b 100644 --- a/models/doctype/AccountingSettings/AccountingSettings.js +++ b/models/doctype/AccountingSettings/AccountingSettings.js @@ -73,7 +73,10 @@ module.exports = { fieldname: 'email', label: 'Email', fieldtype: 'Data', - required: 1 + required: 1, + validate: { + type: 'email' + } }, { diff --git a/models/doctype/Party/Party.js b/models/doctype/Party/Party.js index d49cdf3e..193e09e8 100644 --- a/models/doctype/Party/Party.js +++ b/models/doctype/Party/Party.js @@ -65,13 +65,19 @@ module.exports = { fieldname: 'email', label: 'Email', fieldtype: 'Data', - placeholder: 'john@doe.com' + placeholder: 'john@doe.com', + validate: { + type: 'email' + } }, { fieldname: 'phone', label: 'Phone', fieldtype: 'Data', - placeholder: 'Phone' + placeholder: 'Phone', + validate: { + type: 'phone' + } }, { fieldname: 'address', diff --git a/models/doctype/PrintSettings/PrintSettings.js b/models/doctype/PrintSettings/PrintSettings.js index 665b7efa..79f67ead 100644 --- a/models/doctype/PrintSettings/PrintSettings.js +++ b/models/doctype/PrintSettings/PrintSettings.js @@ -18,7 +18,10 @@ module.exports = { fieldname: 'email', label: 'Email', fieldtype: 'Data', - placeholder: 'john@doe.com' + placeholder: 'john@doe.com', + validate: { + type: 'email' + } }, { fieldname: 'displayLogo', @@ -29,7 +32,10 @@ module.exports = { fieldname: 'phone', label: 'Phone', fieldtype: 'Data', - placeholder: '9888900000' + placeholder: '9888900000', + validate: { + type: 'phone' + } }, { fieldname: 'address', diff --git a/models/doctype/SetupWizard/SetupWizard.js b/models/doctype/SetupWizard/SetupWizard.js index 16949321..a0344e72 100644 --- a/models/doctype/SetupWizard/SetupWizard.js +++ b/models/doctype/SetupWizard/SetupWizard.js @@ -39,7 +39,9 @@ module.exports = { fieldtype: 'Data', placeholder: 'john@doe.com', required: 1, - inputType: 'email' + validate: { + type: 'email' + } }, { diff --git a/src/components/TwoColumnForm.vue b/src/components/TwoColumnForm.vue index 94b28792..3f2b8f12 100644 --- a/src/components/TwoColumnForm.vue +++ b/src/components/TwoColumnForm.vue @@ -132,7 +132,7 @@ let TwoColumnForm = { return this.doc.rename(value); } - this.doc.set(df.fieldname, value); + this.doc.set(df.fieldname, value).catch(this.handleError); if (this.autosave && this.doc._dirty && !this.doc.isNew()) { if (df.fieldtype === 'Table') { diff --git a/src/utils.js b/src/utils.js index 355ff481..0c2500f2 100644 --- a/src/utils.js +++ b/src/utils.js @@ -18,7 +18,24 @@ export function createNewDatabase() { if (!filePath.endsWith('.db')) { filePath = filePath + '.db'; } - resolve(filePath); + if (fs.existsSync(filePath)) { + showMessageDialog({ + // prettier-ignore + message: _('A file exists with the same name and it will be overwritten. Are you sure you want to continue?'), + buttons: [ + { + label: _('Overwrite'), + action() { + fs.unlinkSync(filePath); + resolve(filePath); + } + }, + { label: _('Cancel'), action() {} } + ] + }); + } else { + resolve(filePath); + } } } ); @@ -138,7 +155,7 @@ export function openQuickEdit({ doctype, name, hideFields, defaults = {} }) { } export function handleErrorWithDialog(e, doc) { - let errorMessage; + let errorMessage = e.message || _('An error occurred'); if (e.type === frappe.errors.LinkValidationError) { errorMessage = _('{0} {1} is linked with existing records.', [ doc.doctype, @@ -146,8 +163,6 @@ export function handleErrorWithDialog(e, doc) { ]); } else if (e.type === frappe.errors.DuplicateEntryError) { errorMessage = _('{0} {1} already exists.', [doc.doctype, doc.name]); - } else { - errorMessage = _('An error occurred.'); } showMessageDialog({