diff --git a/src/components/FormModel.vue b/src/components/FormModel.vue index c4d9a4149..1fec1e6c9 100644 --- a/src/components/FormModel.vue +++ b/src/components/FormModel.vue @@ -13,13 +13,12 @@ import VnConfirm from './ui/VnConfirm.vue'; import { tMobile } from 'src/composables/tMobile'; import { useArrayData } from 'src/composables/useArrayData'; import { getDifferences, getUpdatedValues } from 'src/filters'; - const { push } = useRouter(); const quasar = useQuasar(); const state = useState(); const stateStore = useStateStore(); const { t } = useI18n(); -const { validate } = useValidator(); +const { validate, validations } = useValidator(); const { notify } = useNotify(); const route = useRoute(); const myForm = ref(null); @@ -119,7 +118,7 @@ const defaultButtons = computed(() => ({ color: 'primary', icon: 'save', label: 'globals.save', - click: async () => await save(), + click: async (evt) => submitForm(evt), type: 'submit', }, reset: { @@ -132,6 +131,13 @@ const defaultButtons = computed(() => ({ ...$props.defaultButtons, })); +const submitForm = async (evt) => { + const isFormValid = await myForm.value.validate(); + if (isFormValid) { + await save(evt); + } +}; + onMounted(async () => { nextTick(() => (componentIsRendered.value = true)); @@ -227,10 +233,9 @@ async function save() { const method = $props.urlCreate ? 'post' : 'patch'; const url = $props.urlCreate || $props.urlUpdate || $props.url || arrayData.store.url; - let response; - - if ($props.saveFn) response = await $props.saveFn(body); - else response = await axios[method](url, body); + const response = await Promise.resolve( + $props.saveFn ? $props.saveFn(body) : axios[method](url, body), + ); if ($props.urlCreate) notify('globals.dataCreated', 'positive'); @@ -307,11 +312,13 @@ async function onKeyup(evt) { selectionStart = selectionEnd = selectionStart + 1; return; } - await save(); + await myForm.value.submit(evt); } } defineExpose({ + submitForm, + myForm, save, isLoading, hasChanges, @@ -325,7 +332,7 @@ defineExpose({ diff --git a/src/components/FormModelPopup.vue b/src/components/FormModelPopup.vue index 85943e91e..34aec96d8 100644 --- a/src/components/FormModelPopup.vue +++ b/src/components/FormModelPopup.vue @@ -41,9 +41,12 @@ const onDataSaved = async (formData, requestResponse) => { emit('onDataSaved', formData, requestResponse); }; -const onClick = async (saveAndContinue) => { +const onClick = async (saveAndContinue = showSaveAndContinueBtn) => { + await formModelRef.value.myForm.validate(true); isSaveAndContinue.value = saveAndContinue; - await formModelRef.value.save(); + if (formModelRef.value) { + await formModelRef.value.submitForm(); + } }; defineExpose({ @@ -59,16 +62,23 @@ defineExpose({ ref="formModelRef" :observe-form-changes="false" :default-actions="false" + @submit="onClick" v-bind="$attrs" @on-data-saved="onDataSaved" + :prevent-submit="false" > -