diff --git a/webapp/src/components/BatchCreateItemModal.vue b/webapp/src/components/BatchCreateItemModal.vue index 473467838..99ccf60c3 100644 --- a/webapp/src/components/BatchCreateItemModal.vue +++ b/webapp/src/components/BatchCreateItemModal.vue @@ -3,7 +3,7 @@ - + @@ -332,6 +332,7 @@ import Modal from "@/components/Modal.vue"; import ItemSelect from "@/components/ItemSelect.vue"; import { createNewSamples } from "@/server_fetch_utils.js"; +import { validateEntryID } from "@/field_utils.js"; import { itemTypes, SAMPLE_TABLE_TYPES } from "@/resources.js"; export default { name: "BatchCreateItemModal", @@ -414,16 +415,12 @@ export default { ? this.$store.state.sample_list.map((x) => x.item_id) : []; }, - someValidationMessagePresent() { - return this.itemIDValidationMessages.some(); - }, - itemIDValidationMessages() { + isValidEntryID() { return this.items.map((item, index, items) => { if (item.item_id == null) { return ""; - } // Don't throw an error before the user starts typing - - // check that item id isn't repeated in this table + } + // Check if item ID is repeated in the table if ( items .slice(0, index) @@ -433,25 +430,7 @@ export default { return "ID is repeated from an above row."; } - if ( - this.takenItemIds.includes(item.item_id) || - this.takenSampleIds.includes(item.item_id) - ) { - return `${item.item_id} already in use.`; - } - if (!/^[a-zA-Z0-9._-]+$/.test(item.item_id)) { - return "ID can only contain alphanumeric characters, dashes ('-') and underscores ('_') and periods ('.')"; - } - if (/^[._-]/.test(item.item_id) | /[._-]$/.test(item.item_id)) { - return "ID cannot start or end with puncutation"; - } - if (/\s/.test(item.item_id)) { - return "ID cannot have any spaces"; - } - if (item.item_id.length < 1 || item.item_id.length > 40) { - return "ID must be between 1 and 40 characters in length"; - } - return ""; + return validateEntryID(item.item_id, this.takenItemIds, this.takenSampleIds); }); }, }, diff --git a/webapp/src/components/CollectionSelect.vue b/webapp/src/components/CollectionSelect.vue index 5906111b6..539c8fcbf 100644 --- a/webapp/src/components/CollectionSelect.vue +++ b/webapp/src/components/CollectionSelect.vue @@ -9,7 +9,7 @@ @search="debouncedAsyncSearch" > @@ -39,6 +39,7 @@ import vSelect from "vue-select"; import FormattedCollectionName from "@/components/FormattedCollectionName.vue"; import { searchCollections, createNewCollection } from "@/server_fetch_utils.js"; +import { validateEntryID } from "@/field_utils.js"; import { debounceTime } from "@/resources.js"; export default { @@ -82,7 +83,7 @@ export default { this.searchQuery && !this.collections.some((item) => item.collection_id === this.searchQuery) && !valueSafe.some((item) => item.collection_id === this.searchQuery) && - !this.IDValidationMessage() + !this.isValidEntryID ) { return [ ...this.collections, @@ -94,8 +95,8 @@ export default { } return this.collections; }, - validationMessage() { - return this.IDValidationMessage(); + isValidEntryID() { + return validateEntryID(this.searchQuery); }, }, methods: { @@ -126,20 +127,8 @@ export default { loading(false); }, debounceTime); }, - IDValidationMessage() { - if (this.searchQuery && !/^[a-zA-Z0-9_-]+$/.test(this.searchQuery)) { - return "ID can only contain alphanumeric characters, dashes ('-'), and underscores ('_')."; - } - if (/^[._-]/.test(this.searchQuery) | /[._-]$/.test(this.searchQuery)) { - return "ID cannot start or end with punctuation"; - } - if ((this.searchQuery && this.searchQuery.length < 1) || this.searchQuery.length > 40) { - return "ID must be between 1 and 40 characters."; - } - return ""; - }, async handleCreateNewCollection() { - if (this.IDValidationMessage()) { + if (this.isValidEntryID) { return; } else { try { diff --git a/webapp/src/components/CreateCollectionModal.vue b/webapp/src/components/CreateCollectionModal.vue index 8741b8ccd..33e2eb71f 100644 --- a/webapp/src/components/CreateCollectionModal.vue +++ b/webapp/src/components/CreateCollectionModal.vue @@ -2,7 +2,7 @@
@@ -19,7 +19,7 @@ required /> -
+
@@ -47,6 +47,7 @@ import Modal from "@/components/Modal.vue"; import ItemSelect from "@/components/ItemSelect.vue"; import { createNewCollection } from "@/server_fetch_utils.js"; +import { validateEntryID } from "@/field_utils.js"; export default { name: "CreateCollectionModal", @@ -72,21 +73,8 @@ export default { ? this.$store.state.collection_list.map((x) => x.collection_id) : []; }, - IDValidationMessage() { - if (this.collection_id == null) { - return ""; - } // Don't throw an error before the user starts typing - - if (this.takenCollectionIds.includes(this.collection_id)) { - return `${this.collection_id} already in use.`; - } - if (!/^[a-zA-Z0-9_-]+$/.test(this.collection_id)) { - return "ID can only contain alphanumeric characters, dashes ('-') and underscores ('_')"; - } - if (this.collection_id.length < 1 || this.collection_id.length > 40) { - return "ID must be between 1 and 40 characters in length"; - } - return ""; + isValidEntryID() { + return validateEntryID(this.collection_id, this.takenCollectionIds); }, }, methods: { diff --git a/webapp/src/components/CreateEquipmentModal.vue b/webapp/src/components/CreateEquipmentModal.vue index d90ae74ad..2136cd495 100644 --- a/webapp/src/components/CreateEquipmentModal.vue +++ b/webapp/src/components/CreateEquipmentModal.vue @@ -2,7 +2,7 @@ @@ -13,7 +13,7 @@ -
+