2020 <button
2121 @click =" () => saveRecord()"
2222 class =" af-save-button flex items-center py-1 px-3 text-sm font-medium rounded-default text-lightCreateViewSaveButtonText focus:outline-none bg-lightCreateViewButtonBackground rounded border border-lightCreateViewButtonBorder hover:bg-lightCreateViewButtonBackgroundHover hover:text-lightCreateViewSaveButtonTextHover focus:z-10 focus:ring-4 focus:ring-lightCreateViewButtonFocusRing dark:focus:ring-darkCreateViewButtonFocusRing dark:bg-darkCreateViewButtonBackground dark:text-darkCreateViewSaveButtonText dark:border-darkCreateViewButtonBorder dark:hover:text-darkCreateViewSaveButtonTextHover dark:hover:bg-darkCreateViewButtonBackgroundHover disabled:opacity-50 gap-1"
23- :disabled =" saving || (validating && !isValid)"
23+ :disabled =" saving || (validatingMode && !isValid) || resourceFormRef?.isValidating "
2424 >
2525 <svg v-if =" saving"
2626 aria-hidden="true" class="w-4 h-4 mr-1 text-gray-200 animate-spin dark:text-gray-600 fill-lightCreateViewSaveButtonText" viewBox="0 0 100 101" fill="none" xmlns="http://www.w3.org/2000/svg"><path d="M100 50.5908C100 78.2051 77.6142 100.591 50 100.591C22.3858 100.591 0 78.2051 0 50.5908C0 22.9766 22.3858 0.59082 50 0.59082C77.6142 0.59082 100 22.9766 100 50.5908ZM9.08144 50.5908C9.08144 73.1895 27.4013 91.5094 50 91.5094C72.5987 91.5094 90.9186 73.1895 90.9186 50.5908C90.9186 27.9921 72.5987 9.67226 50 9.67226C27.4013 9.67226 9.08144 27.9921 9.08144 50.5908Z" fill="currentColor"/><path d="M93.9676 39.0409C96.393 38.4038 97.8624 35.9116 97.0079 33.5539C95.2932 28.8227 92.871 24.3692 89.8167 20.348C85.8452 15.1192 80.8826 10.7238 75.2124 7.41289C69.5422 4.10194 63.2754 1.94025 56.7698 1.05124C51.7666 0.367541 46.6976 0.446843 41.7345 1.27873C39.2613 1.69328 37.813 4.19778 38.4501 6.62326C39.0873 9.04874 41.5694 10.4717 44.0505 10.1071C47.8511 9.54855 51.7191 9.52689 55.5402 10.0491C60.8642 10.7766 65.9928 12.5457 70.6331 15.2552C75.2735 17.9648 79.3347 21.5619 82.5849 25.841C84.9175 28.9121 86.7997 32.2913 88.1811 35.8758C89.083 38.2158 91.5421 39.6781 93.9676 39.0409Z" fill="currentFill"/></svg>
5454 :resource =" coreStore.resource!"
5555 @update:record =" onUpdateRecord"
5656 @update:isValid =" isValid = $event"
57- :validating = " validating "
57+ :validatingMode = " validatingMode "
5858 :source =" 'create'"
5959 :readonlyColumns =" readonlyColumns"
6060 >
@@ -90,9 +90,10 @@ import { useAdminforth } from '@/adminforth';
9090import { useI18n } from ' vue-i18n' ;
9191import { type AdminForthComponentDeclarationFull } from ' @/types/Common.js' ;
9292import type { AdminForthResourceColumn } from ' @/types/Back' ;
93+ import { scrollToInvalidField , saveRecordPreparations } from ' @/utils' ;
9394
9495const isValid = ref (false );
95- const validating = ref (false );
96+ const validatingMode = ref (false );
9697
9798const loading = ref (true );
9899const saving = ref (false );
@@ -195,28 +196,23 @@ onMounted(async () => {
195196});
196197
197198async function saveRecord() {
198- if (! isValid .value ) {
199- validating .value = true ;
200- await nextTick ();
201- scrollToInvalidField ();
202- return ;
203- } else {
204- validating .value = false ;
205- }
206199 const requiredColumns = coreStore .resource ?.columns .filter (c => c .required ?.create === true ) || [];
207200 const requiredColumnsToSkip = requiredColumns .filter (c => checkShowIf (c , record .value , coreStore .resource ?.columns || []) === false );
208201 saving .value = true ;
209- const interceptorsResult = await runSaveInterceptors ({
210- action: ' create' ,
211- values: record .value ,
212- resource: coreStore .resource ,
213- resourceId: route .params .resourceId as string ,
214- });
215- if (! interceptorsResult .ok ) {
216- saving .value = false ;
217- if (interceptorsResult .error ) showErrorTost (interceptorsResult .error );
218- return ;
219- }
202+
203+ const interceptorsResult = await saveRecordPreparations (
204+ ' create' ,
205+ validatingMode ,
206+ resourceFormRef ,
207+ isValid ,
208+ t ,
209+ saving ,
210+ runSaveInterceptors ,
211+ record ,
212+ coreStore ,
213+ route
214+ );
215+
220216 const interceptorConfirmationResult = (interceptorsResult .extra as Record <string , any >)?.confirmationResult ;
221217 const response = await callAdminForthApi ({
222218 method: ' POST' ,
@@ -254,23 +250,4 @@ async function saveRecord() {
254250 saving .value = false ;
255251}
256252
257- function scrollToInvalidField() {
258- let columnsWithErrors: {column: AdminForthResourceColumn , error: string }[] = [];
259- for (const column of resourceFormRef .value ?.editableColumns || []) {
260- const error = resourceFormRef .value ?.columnError (column );
261- if (error ) {
262- columnsWithErrors .push ({column , error });
263- }
264- }
265- const errorMessage = t (' Failed to save. Please fix errors for the following fields:' ) + ' <ul class="mt-2 list-disc list-inside">' + columnsWithErrors .map (c => ` <li><strong>${c .column .label || c .column .name }</strong>: ${c .error }</li> ` ).join (' ' ) + ' </ul>' ;
266- alert ({
267- messageHtml: errorMessage ,
268- variant: ' danger'
269- });
270- const firstInvalidElement = document .querySelector (' .af-invalid-field-message' );
271- if (firstInvalidElement ) {
272- firstInvalidElement .scrollIntoView ({ behavior: ' smooth' , block: ' center' });
273- }
274- }
275-
276253 </script >
0 commit comments