From a55ffffe79810791e846e009ff520731721f10ea Mon Sep 17 00:00:00 2001 From: Yuri Kuznetsov Date: Fri, 17 Nov 2023 14:29:12 +0200 Subject: [PATCH] barcode frontend validation --- .../Espo/Resources/i18n/en_US/Global.json | 1 + client/src/views/fields/barcode.js | 59 ++++++++++++++++--- 2 files changed, 53 insertions(+), 7 deletions(-) diff --git a/application/Espo/Resources/i18n/en_US/Global.json b/application/Espo/Resources/i18n/en_US/Global.json index 5986c3b5a9..5ab6d64099 100644 --- a/application/Espo/Resources/i18n/en_US/Global.json +++ b/application/Espo/Resources/i18n/en_US/Global.json @@ -326,6 +326,7 @@ "fieldValueDuplicate": "Duplicate value", "fieldIsUploading": "Uploading in progress", "fieldExceedsMaxCount": "Count exceeds max allowed {maxCount}", + "barcodeInvalid": "{field} is not valid {type}", "arrayItemMaxLength": "Item shouldn't be longer than {max} characters", "resetPreferencesDone": "Preferences has been reset to defaults", "confirmation": "Are you sure?", diff --git a/client/src/views/fields/barcode.js b/client/src/views/fields/barcode.js index b575286970..e4b3ead413 100644 --- a/client/src/views/fields/barcode.js +++ b/client/src/views/fields/barcode.js @@ -39,6 +39,8 @@ class BarcodeFieldView extends VarcharFieldView { detailTemplate = 'fields/barcode/detail' setup() { + this.validations.push('valid'); + let maxLength = 255; // noinspection SpellCheckingInspection @@ -186,18 +188,61 @@ class BarcodeFieldView extends VarcharFieldView { } initBarcode(value) { - JsBarcode(this.getSelector() + ' .barcode', value, { - format: this.params.codeType, - height: 50, - fontSize: 14, - margin: 0, - lastChar: this.params.lastChar, - }); + try { + JsBarcode(this.getSelector() + ' .barcode', value, { + format: this.params.codeType, + height: 50, + fontSize: 14, + margin: 0, + lastChar: this.params.lastChar, + }); + } + catch (e) { + console.error(this.name, e); + } } controlWidth() { this.$el.find('.barcode').css('max-width', this.$el.width() + 'px'); } + + // noinspection JSUnusedGlobalSymbols + validateValid() { + if (this.params.codeType === 'QRcode') { + return; + } + + const value = this.model.get(this.name); + + if (!value) { + return; + } + + let isValid; + + try { + JsBarcode({}, value, { + format: this.params.codeType, + lastChar: this.params.lastChar, + valid: valid => isValid = valid, + }); + } + catch (e) { + return true; + } + + if (isValid) { + return; + } + + const msg = this.translate('barcodeInvalid', 'messages') + .replace('{field}', this.getLabelText()) + .replace('{type}', this.params.codeType); + + this.showValidationMessage(msg); + + return true; + } } export default BarcodeFieldView;