From fa0ca732fc7e7b9ec3ab4b108719b66e64ea795e Mon Sep 17 00:00:00 2001 From: alexm Date: Fri, 16 Aug 2024 12:29:35 +0200 Subject: [PATCH] feat(FormModel): trim data by default --- src/components/FormModel.vue | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/src/components/FormModel.vue b/src/components/FormModel.vue index a0f6bf479..22ef1622c 100644 --- a/src/components/FormModel.vue +++ b/src/components/FormModel.vue @@ -87,6 +87,10 @@ const $props = defineProps({ type: Boolean, default: false, }, + defaultTrim: { + type: Boolean, + default: true, + }, }); const emit = defineEmits(['onFetch', 'onDataSaved']); const modelValue = computed( @@ -195,6 +199,7 @@ async function save() { isLoading.value = true; try { + formData.value = trimData(formData.value); const body = $props.mapper ? $props.mapper(formData.value) : formData.value; const method = $props.urlCreate ? 'post' : 'patch'; const url = @@ -253,6 +258,14 @@ function updateAndEmit(evt, val, res) { emit(evt, state.get(modelValue), res); } +function trimData(data) { + if (!$props.defaultTrim) return data; + for (const key in data) { + if (typeof data[key] == 'string') data[key] = data[key].trim(); + } + return data; +} + defineExpose({ save, isLoading,