adminforth 2.27.0-next.7 → 2.27.0-next.9
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/modules/restApi.d.ts.map +1 -1
- package/dist/modules/restApi.js +40 -2
- package/dist/modules/restApi.js.map +1 -1
- package/dist/spa/package-lock.json +41 -0
- package/dist/spa/package.json +3 -0
- package/dist/spa/pnpm-lock.yaml +38 -0
- package/dist/spa/src/components/ColumnValueInputWrapper.vue +24 -1
- package/dist/spa/src/components/GroupsTable.vue +7 -4
- package/dist/spa/src/components/ResourceForm.vue +100 -6
- package/dist/spa/src/components/ThreeDotsMenu.vue +6 -3
- package/dist/spa/src/types/Common.ts +17 -3
- package/dist/spa/src/utils/createEditUtils.ts +65 -0
- package/dist/spa/src/utils/index.ts +2 -1
- package/dist/spa/src/utils/utils.ts +7 -4
- package/dist/spa/src/utils.ts +2 -1
- package/dist/spa/src/views/CreateView.vue +22 -49
- package/dist/spa/src/views/EditView.vue +20 -38
- package/dist/types/Common.d.ts +22 -3
- package/dist/types/Common.d.ts.map +1 -1
- package/dist/types/Common.js.map +1 -1
- package/package.json +1 -1
|
@@ -13,11 +13,13 @@
|
|
|
13
13
|
:mode="mode"
|
|
14
14
|
:unmasked="unmasked"
|
|
15
15
|
:columnOptions="columnOptions"
|
|
16
|
-
:
|
|
16
|
+
:validatingMode="validatingMode"
|
|
17
17
|
:columnError="columnError"
|
|
18
18
|
:setCurrentValue="setCurrentValue"
|
|
19
19
|
@update:customComponentsInValidity="(data) => customComponentsInValidity = { ...customComponentsInValidity, ...data }"
|
|
20
20
|
@update:customComponentsEmptiness="(data) => customComponentsEmptiness = { ...customComponentsEmptiness, ...data }"
|
|
21
|
+
:columnsWithErrors="columnsWithErrors"
|
|
22
|
+
:isValidating="isValidating"
|
|
21
23
|
/>
|
|
22
24
|
</div>
|
|
23
25
|
<div v-else class="flex flex-col gap-4">
|
|
@@ -31,11 +33,13 @@
|
|
|
31
33
|
:mode="mode"
|
|
32
34
|
:unmasked="unmasked"
|
|
33
35
|
:columnOptions="columnOptions"
|
|
34
|
-
:
|
|
36
|
+
:validatingMode="validatingMode"
|
|
35
37
|
:columnError="columnError"
|
|
36
38
|
:setCurrentValue="setCurrentValue"
|
|
37
39
|
@update:customComponentsInValidity="(data) => customComponentsInValidity = { ...customComponentsInValidity, ...data }"
|
|
38
40
|
@update:customComponentsEmptiness="(data) => customComponentsEmptiness = { ...customComponentsEmptiness, ...data }"
|
|
41
|
+
:columnsWithErrors="columnsWithErrors"
|
|
42
|
+
:isValidating="isValidating"
|
|
39
43
|
/>
|
|
40
44
|
</template>
|
|
41
45
|
<div v-if="otherColumns?.length || 0 > 0">
|
|
@@ -48,11 +52,13 @@
|
|
|
48
52
|
:mode="mode"
|
|
49
53
|
:unmasked="unmasked"
|
|
50
54
|
:columnOptions="columnOptions"
|
|
51
|
-
:
|
|
55
|
+
:validatingMode="validatingMode"
|
|
52
56
|
:columnError="columnError"
|
|
53
57
|
:setCurrentValue="setCurrentValue"
|
|
54
58
|
@update:customComponentsInValidity="(data) => customComponentsInValidity = { ...customComponentsInValidity, ...data }"
|
|
55
59
|
@update:customComponentsEmptiness="(data) => customComponentsEmptiness = { ...customComponentsEmptiness, ...data }"
|
|
60
|
+
:columnsWithErrors="columnsWithErrors"
|
|
61
|
+
:isValidating="isValidating"
|
|
56
62
|
/>
|
|
57
63
|
</div>
|
|
58
64
|
</div>
|
|
@@ -71,16 +77,20 @@ import { useCoreStore } from "@/stores/core";
|
|
|
71
77
|
import GroupsTable from '@/components/GroupsTable.vue';
|
|
72
78
|
import { useI18n } from 'vue-i18n';
|
|
73
79
|
import { type AdminForthResourceColumnCommon, type AdminForthResourceCommon } from '@/types/Common';
|
|
80
|
+
import { Mutex } from 'async-mutex';
|
|
81
|
+
import debounce from 'lodash.debounce';
|
|
74
82
|
|
|
75
83
|
const { t } = useI18n();
|
|
76
84
|
|
|
85
|
+
const mutex = new Mutex();
|
|
86
|
+
|
|
77
87
|
const coreStore = useCoreStore();
|
|
78
88
|
const router = useRouter();
|
|
79
89
|
const route = useRoute();
|
|
80
90
|
const props = defineProps<{
|
|
81
91
|
resource: AdminForthResourceCommon,
|
|
82
92
|
record: any,
|
|
83
|
-
|
|
93
|
+
validatingMode: boolean,
|
|
84
94
|
source: 'create' | 'edit',
|
|
85
95
|
readonlyColumns?: string[],
|
|
86
96
|
}>();
|
|
@@ -99,6 +109,11 @@ const columnOptions = ref<Record<string, any[]>>({});
|
|
|
99
109
|
const columnLoadingState = reactive<Record<string, { loading: boolean; hasMore: boolean }>>({});
|
|
100
110
|
const columnOffsets = reactive<Record<string, number>>({});
|
|
101
111
|
const columnEmptyResultsCount = reactive<Record<string, number>>({});
|
|
112
|
+
const columnsWithErrors = ref<Record<string, string>>({});
|
|
113
|
+
const isValidating = ref(false);
|
|
114
|
+
const blockSettingIsValidating = ref(false);
|
|
115
|
+
const isValid = ref(true);
|
|
116
|
+
const doesUserHaveCustomValidation = computed(() => props.resource.columns.some(column => column.validation && column.validation.some((val) => val.validator)));
|
|
102
117
|
|
|
103
118
|
const columnError = (column: AdminForthResourceColumnCommon) => {
|
|
104
119
|
const val = computed(() => {
|
|
@@ -329,10 +344,48 @@ const editableColumns = computed(() => {
|
|
|
329
344
|
return props.resource?.columns?.filter(column => column.showIn?.[mode.value] && (currentValues.value ? checkShowIf(column, currentValues.value, props.resource.columns) : true));
|
|
330
345
|
});
|
|
331
346
|
|
|
332
|
-
|
|
333
|
-
|
|
347
|
+
function checkIfColumnHasError(column: AdminForthResourceColumnCommon) {
|
|
348
|
+
const error = columnError(column);
|
|
349
|
+
if (error) {
|
|
350
|
+
columnsWithErrors.value[column.name] = error;
|
|
351
|
+
} else {
|
|
352
|
+
delete columnsWithErrors.value[column.name];
|
|
353
|
+
}
|
|
354
|
+
}
|
|
355
|
+
|
|
356
|
+
const checkIfAnyColumnHasErrors = () => {
|
|
357
|
+
return Object.keys(columnsWithErrors.value).length > 0 ? false : true;
|
|
358
|
+
}
|
|
359
|
+
|
|
360
|
+
const debouncedValidation = debounce(async (columns: AdminForthResourceColumnCommon[]) => {
|
|
361
|
+
await mutex.runExclusive(async () => {
|
|
362
|
+
await validateUsingUserValidationFunction(columns);
|
|
363
|
+
});
|
|
364
|
+
setIsValidatingValue(false);
|
|
365
|
+
isValid.value = checkIfAnyColumnHasErrors();
|
|
366
|
+
}, 500);
|
|
367
|
+
|
|
368
|
+
watch(() => [editableColumns.value, props.validatingMode], async () => {
|
|
369
|
+
setIsValidatingValue(true);
|
|
370
|
+
|
|
371
|
+
editableColumns.value?.forEach(column => {
|
|
372
|
+
checkIfColumnHasError(column);
|
|
373
|
+
});
|
|
374
|
+
|
|
375
|
+
if (props.validatingMode && doesUserHaveCustomValidation.value) {
|
|
376
|
+
debouncedValidation(editableColumns.value);
|
|
377
|
+
} else {
|
|
378
|
+
setIsValidatingValue(false);
|
|
379
|
+
isValid.value = checkIfAnyColumnHasErrors();
|
|
380
|
+
}
|
|
334
381
|
});
|
|
335
382
|
|
|
383
|
+
const setIsValidatingValue = (value: boolean) => {
|
|
384
|
+
if (!blockSettingIsValidating.value) {
|
|
385
|
+
isValidating.value = value;
|
|
386
|
+
}
|
|
387
|
+
}
|
|
388
|
+
|
|
336
389
|
|
|
337
390
|
const groups = computed(() => {
|
|
338
391
|
let fieldGroupType;
|
|
@@ -381,9 +434,50 @@ watch(() => isValid.value, (value) => {
|
|
|
381
434
|
emit('update:isValid', value);
|
|
382
435
|
});
|
|
383
436
|
|
|
437
|
+
async function validateUsingUserValidationFunction(editableColumnsInner: AdminForthResourceColumnCommon[]): Promise<void> {
|
|
438
|
+
if (doesUserHaveCustomValidation.value) {
|
|
439
|
+
try {
|
|
440
|
+
blockSettingIsValidating.value = true;
|
|
441
|
+
const res = await callAdminForthApi({
|
|
442
|
+
method: 'POST',
|
|
443
|
+
path: '/validate_columns',
|
|
444
|
+
body: {
|
|
445
|
+
resourceId: props.resource.resourceId,
|
|
446
|
+
editableColumns: editableColumnsInner.map(col => {return {name: col.name, value: currentValues.value?.[col.name]} }),
|
|
447
|
+
record: currentValues.value,
|
|
448
|
+
}
|
|
449
|
+
})
|
|
450
|
+
if (res.validationResults && Object.keys(res.validationResults).length > 0) {
|
|
451
|
+
for (const [columnName, validationResult] of Object.entries(res.validationResults) as [string, any][]) {
|
|
452
|
+
if (!validationResult.isValid) {
|
|
453
|
+
columnsWithErrors.value[columnName] = validationResult.message || 'Invalid value';
|
|
454
|
+
} else {
|
|
455
|
+
delete columnsWithErrors.value[columnName];
|
|
456
|
+
}
|
|
457
|
+
}
|
|
458
|
+
const columnsToProcess = editableColumns.value.filter(col => res.validationResults[col.name] === undefined);
|
|
459
|
+
columnsToProcess.forEach(column => {
|
|
460
|
+
checkIfColumnHasError(column);
|
|
461
|
+
});
|
|
462
|
+
} else {
|
|
463
|
+
editableColumnsInner.forEach(column => {
|
|
464
|
+
checkIfColumnHasError(column);
|
|
465
|
+
});
|
|
466
|
+
}
|
|
467
|
+
blockSettingIsValidating.value = false;
|
|
468
|
+
} catch (e) {
|
|
469
|
+
console.error('Error during custom validation', e);
|
|
470
|
+
blockSettingIsValidating.value = false;
|
|
471
|
+
}
|
|
472
|
+
}
|
|
473
|
+
}
|
|
474
|
+
|
|
384
475
|
defineExpose({
|
|
385
476
|
columnError,
|
|
386
477
|
editableColumns,
|
|
478
|
+
columnsWithErrors,
|
|
479
|
+
isValidating,
|
|
480
|
+
validateUsingUserValidationFunction
|
|
387
481
|
})
|
|
388
482
|
|
|
389
483
|
</script>
|
|
@@ -47,10 +47,10 @@
|
|
|
47
47
|
<div class="wrapper">
|
|
48
48
|
<component
|
|
49
49
|
:is="(action.customComponent && getCustomComponent(formatComponent(action.customComponent))) || CallActionWrapper"
|
|
50
|
-
:meta="formatComponent(action.customComponent
|
|
50
|
+
:meta="formatComponent(action.customComponent).meta"
|
|
51
51
|
@callAction="(payload? : Object) => handleActionClick(action, payload)"
|
|
52
52
|
>
|
|
53
|
-
<a @click.prevent class="block
|
|
53
|
+
<a @click.prevent class="block">
|
|
54
54
|
<div class="flex items-center gap-2">
|
|
55
55
|
<component
|
|
56
56
|
v-if="action.icon"
|
|
@@ -196,7 +196,10 @@ onUnmounted(() => {
|
|
|
196
196
|
|
|
197
197
|
<style lang="scss" scoped>
|
|
198
198
|
.wrapper > * {
|
|
199
|
-
@apply px-4 py-2
|
|
199
|
+
@apply px-4 py-2
|
|
200
|
+
hover:text-lightThreeDotsMenuBodyTextHover hover:bg-lightThreeDotsMenuBodyBackgroundHover
|
|
201
|
+
dark:hover:bg-darkThreeDotsMenuBodyBackgroundHover dark:hover:text-darkThreeDotsMenuBodyTextHover
|
|
202
|
+
cursor-pointer;
|
|
200
203
|
}
|
|
201
204
|
</style>
|
|
202
205
|
|
|
@@ -303,7 +303,7 @@ export interface AdminForthComponentDeclarationFull {
|
|
|
303
303
|
[key: string]: any,
|
|
304
304
|
}
|
|
305
305
|
}
|
|
306
|
-
import { type AdminForthActionInput, type AdminForthResource } from './Back.js'
|
|
306
|
+
import { type IAdminForth, type AdminForthActionInput, type AdminForthResource } from './Back.js'
|
|
307
307
|
export { type AdminForthActionInput } from './Back.js'
|
|
308
308
|
|
|
309
309
|
export type AdminForthComponentDeclaration = AdminForthComponentDeclarationFull | string;
|
|
@@ -610,14 +610,14 @@ export type ValidationObject = {
|
|
|
610
610
|
* ```
|
|
611
611
|
*
|
|
612
612
|
*/
|
|
613
|
-
regExp
|
|
613
|
+
regExp?: string,
|
|
614
614
|
|
|
615
615
|
/**
|
|
616
616
|
* Error message shown to user if validation fails
|
|
617
617
|
*
|
|
618
618
|
* Example: "Invalid email format"
|
|
619
619
|
*/
|
|
620
|
-
message
|
|
620
|
+
message?: string,
|
|
621
621
|
|
|
622
622
|
/**
|
|
623
623
|
* Whether to check case sensitivity (i flag)
|
|
@@ -633,6 +633,20 @@ export type ValidationObject = {
|
|
|
633
633
|
* Whether to check global strings (g flag)
|
|
634
634
|
*/
|
|
635
635
|
global?: boolean
|
|
636
|
+
|
|
637
|
+
/**
|
|
638
|
+
* Custom validator function.
|
|
639
|
+
*
|
|
640
|
+
* Example:
|
|
641
|
+
*
|
|
642
|
+
* ```ts
|
|
643
|
+
* validator: async (value) => {
|
|
644
|
+
* // custom validation logic
|
|
645
|
+
* return { isValid: true, message: 'Validation passed' }; // or { isValid: false, message: 'Validation failed' }
|
|
646
|
+
* }
|
|
647
|
+
* ```
|
|
648
|
+
*/
|
|
649
|
+
validator?: (value: any, record: any, adminForth: IAdminForth) => {isValid: boolean, message?: string} | Promise<{isValid: boolean, message?: string}> | boolean,
|
|
636
650
|
}
|
|
637
651
|
|
|
638
652
|
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
import type { AdminForthResourceColumn } from '@/types/Back';
|
|
2
|
+
import { useAdminforth } from '@/adminforth';
|
|
3
|
+
import { type Ref, nextTick } from 'vue';
|
|
4
|
+
|
|
5
|
+
export function scrollToInvalidField(resourceFormRef: any, t: (key: string) => string) {
|
|
6
|
+
const { alert } = useAdminforth();
|
|
7
|
+
let columnsWithErrors: {column: AdminForthResourceColumn, error: string}[] = [];
|
|
8
|
+
for (const column of resourceFormRef.value?.editableColumns || []) {
|
|
9
|
+
if (resourceFormRef.value?.columnsWithErrors[column.name]) {
|
|
10
|
+
columnsWithErrors.push({
|
|
11
|
+
column,
|
|
12
|
+
error: resourceFormRef.value?.columnsWithErrors[column.name]
|
|
13
|
+
});
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
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>';
|
|
17
|
+
alert({
|
|
18
|
+
messageHtml: errorMessage,
|
|
19
|
+
variant: 'danger'
|
|
20
|
+
});
|
|
21
|
+
const firstInvalidElement = document.querySelector('.af-invalid-field-message');
|
|
22
|
+
if (firstInvalidElement) {
|
|
23
|
+
firstInvalidElement.scrollIntoView({ behavior: 'smooth', block: 'center' });
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export async function saveRecordPreparations(
|
|
28
|
+
viewMode: 'create' | 'edit',
|
|
29
|
+
validatingMode: Ref<boolean>,
|
|
30
|
+
resourceFormRef: Ref<any>,
|
|
31
|
+
isValid: Ref<boolean>,
|
|
32
|
+
t: (key: string) => string,
|
|
33
|
+
saving: Ref<boolean>,
|
|
34
|
+
runSaveInterceptors: any,
|
|
35
|
+
record: Ref<Record<string, any>>,
|
|
36
|
+
coreStore: any,
|
|
37
|
+
route: any
|
|
38
|
+
) {
|
|
39
|
+
validatingMode.value = true;
|
|
40
|
+
await nextTick();
|
|
41
|
+
//wait for response for the user validation function if it exists
|
|
42
|
+
while (1) {
|
|
43
|
+
if (resourceFormRef.value?.isValidating) {
|
|
44
|
+
await new Promise(resolve => setTimeout(resolve, 100));
|
|
45
|
+
} else {
|
|
46
|
+
break;
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
if (!isValid.value) {
|
|
50
|
+
await nextTick();
|
|
51
|
+
scrollToInvalidField(resourceFormRef, t);
|
|
52
|
+
return;
|
|
53
|
+
} else {
|
|
54
|
+
validatingMode.value = false;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
saving.value = true;
|
|
58
|
+
const interceptorsResult = await runSaveInterceptors({
|
|
59
|
+
action: viewMode,
|
|
60
|
+
values: record.value,
|
|
61
|
+
resource: coreStore.resource,
|
|
62
|
+
resourceId: route.params.resourceId as string,
|
|
63
|
+
});
|
|
64
|
+
return interceptorsResult;
|
|
65
|
+
}
|
|
@@ -101,11 +101,13 @@ export async function callAdminForthApi(
|
|
|
101
101
|
}
|
|
102
102
|
}
|
|
103
103
|
|
|
104
|
-
export function formatComponent(component: AdminForthComponentDeclaration): AdminForthComponentDeclarationFull {
|
|
104
|
+
export function formatComponent(component: AdminForthComponentDeclaration | undefined): AdminForthComponentDeclarationFull {
|
|
105
105
|
if (typeof component === 'string') {
|
|
106
106
|
return { file: component, meta: {} };
|
|
107
|
-
} else {
|
|
107
|
+
} else if (typeof component === 'object') {
|
|
108
108
|
return { file: component.file, meta: component.meta };
|
|
109
|
+
} else {
|
|
110
|
+
return { file: '', meta: {} };
|
|
109
111
|
}
|
|
110
112
|
}
|
|
111
113
|
|
|
@@ -191,7 +193,8 @@ export function applyRegexValidation(value: any, validation: ValidationObject[]
|
|
|
191
193
|
if ( validation?.length ) {
|
|
192
194
|
const validationArray = validation;
|
|
193
195
|
for (let i = 0; i < validationArray.length; i++) {
|
|
194
|
-
|
|
196
|
+
const regExpPattern = validationArray[i].regExp;
|
|
197
|
+
if (regExpPattern) {
|
|
195
198
|
let flags = '';
|
|
196
199
|
if (validationArray[i].caseSensitive) {
|
|
197
200
|
flags += 'i';
|
|
@@ -203,7 +206,7 @@ export function applyRegexValidation(value: any, validation: ValidationObject[]
|
|
|
203
206
|
flags += 'g';
|
|
204
207
|
}
|
|
205
208
|
|
|
206
|
-
const regExp = new RegExp(
|
|
209
|
+
const regExp = new RegExp(regExpPattern, flags);
|
|
207
210
|
if (value === undefined || value === null) {
|
|
208
211
|
value = '';
|
|
209
212
|
}
|
package/dist/spa/src/utils.ts
CHANGED
|
@@ -20,11 +20,9 @@
|
|
|
20
20
|
<button
|
|
21
21
|
@click="() => saveRecord()"
|
|
22
22
|
class="af-save-button h-[34px] af-button-shadow 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 || (
|
|
23
|
+
:disabled="saving || (validatingMode && !isValid) || resourceFormRef?.isValidating"
|
|
24
24
|
>
|
|
25
|
-
<
|
|
26
|
-
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>
|
|
27
|
-
|
|
25
|
+
<Spinner v-if="saving || resourceFormRef?.isValidating" class="w-4 h-4" />
|
|
28
26
|
<IconFloppyDiskSolid v-else class="w-4 h-4" />
|
|
29
27
|
{{ $t('Save') }}
|
|
30
28
|
</button>
|
|
@@ -54,7 +52,7 @@
|
|
|
54
52
|
:resource="coreStore.resource!"
|
|
55
53
|
@update:record="onUpdateRecord"
|
|
56
54
|
@update:isValid="isValid = $event"
|
|
57
|
-
:
|
|
55
|
+
:validatingMode="validatingMode"
|
|
58
56
|
:source="'create'"
|
|
59
57
|
:readonlyColumns="readonlyColumns"
|
|
60
58
|
>
|
|
@@ -81,18 +79,18 @@ import SingleSkeletLoader from '@/components/SingleSkeletLoader.vue';
|
|
|
81
79
|
import { useCoreStore } from '@/stores/core';
|
|
82
80
|
import { callAdminForthApi, getCustomComponent,checkAcessByAllowedActions, initThreeDotsDropdown, checkShowIf, compareOldAndNewRecord, onBeforeRouteLeaveCreateEditViewGuard, leaveGuardActiveClass, formatComponent } from '@/utils';
|
|
83
81
|
import { IconFloppyDiskSolid } from '@iconify-prerendered/vue-flowbite';
|
|
84
|
-
import { onMounted, onBeforeMount, onBeforeUnmount, ref
|
|
85
|
-
import { useRoute, useRouter
|
|
86
|
-
import { computed } from 'vue';
|
|
82
|
+
import { onMounted, onBeforeMount, onBeforeUnmount, ref } from 'vue';
|
|
83
|
+
import { useRoute, useRouter } from 'vue-router';
|
|
87
84
|
import { showErrorTost } from '@/composables/useFrontendApi';
|
|
88
85
|
import ThreeDotsMenu from '@/components/ThreeDotsMenu.vue';
|
|
89
86
|
import { useAdminforth } from '@/adminforth';
|
|
90
87
|
import { useI18n } from 'vue-i18n';
|
|
91
88
|
import { type AdminForthComponentDeclaration, type AdminForthComponentDeclarationFull } from '@/types/Common.js';
|
|
92
|
-
import
|
|
89
|
+
import { saveRecordPreparations } from '@/utils';
|
|
90
|
+
import { Spinner } from '@/afcl'
|
|
93
91
|
|
|
94
92
|
const isValid = ref(false);
|
|
95
|
-
const
|
|
93
|
+
const validatingMode = ref(false);
|
|
96
94
|
|
|
97
95
|
const loading = ref(true);
|
|
98
96
|
const saving = ref(false);
|
|
@@ -195,28 +193,22 @@ onMounted(async () => {
|
|
|
195
193
|
});
|
|
196
194
|
|
|
197
195
|
async function saveRecord() {
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
196
|
+
const interceptorsResult = await saveRecordPreparations(
|
|
197
|
+
'create',
|
|
198
|
+
validatingMode,
|
|
199
|
+
resourceFormRef,
|
|
200
|
+
isValid,
|
|
201
|
+
t,
|
|
202
|
+
saving,
|
|
203
|
+
runSaveInterceptors,
|
|
204
|
+
record,
|
|
205
|
+
coreStore,
|
|
206
|
+
route
|
|
207
|
+
);
|
|
208
|
+
|
|
206
209
|
const requiredColumns = coreStore.resource?.columns.filter(c => c.required?.create === true) || [];
|
|
207
210
|
const requiredColumnsToSkip = requiredColumns.filter(c => checkShowIf(c, record.value, coreStore.resource?.columns || []) === false);
|
|
208
|
-
|
|
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
|
-
}
|
|
211
|
+
|
|
220
212
|
const interceptorConfirmationResult = (interceptorsResult.extra as Record<string, any>)?.confirmationResult;
|
|
221
213
|
const response = await callAdminForthApi({
|
|
222
214
|
method: 'POST',
|
|
@@ -254,23 +246,4 @@ async function saveRecord() {
|
|
|
254
246
|
saving.value = false;
|
|
255
247
|
}
|
|
256
248
|
|
|
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
|
-
|
|
276
249
|
</script>
|
|
@@ -19,9 +19,10 @@
|
|
|
19
19
|
<button
|
|
20
20
|
@click="() => saveRecord()"
|
|
21
21
|
class="flex items-center h-[34px] af-button-shadow py-1 px-3 text-sm font-medium rounded-default text-lightEditViewSaveButtonText focus:outline-none bg-lightEditViewButtonBackground rounded border border-lightEditViewButtonBorder hover:bg-lightEditViewButtonBackgroundHover hover:text-lightEditViewSaveButtonTextHover focus:z-10 focus:ring-4 focus:ring-lightEditViewButtonFocusRing dark:focus:ring-darkEditViewButtonFocusRing dark:bg-darkEditViewButtonBackground dark:text-darkEditViewSaveButtonText dark:border-darkEditViewButtonBorder dark:hover:text-darkEditViewSaveButtonTextHover dark:hover:bg-darkEditViewButtonBackgroundHover disabled:opacity-50 gap-1"
|
|
22
|
-
:disabled="saving || (
|
|
22
|
+
:disabled="saving || (validatingMode && !isValid) || resourceFormRef?.isValidating"
|
|
23
23
|
>
|
|
24
|
-
<
|
|
24
|
+
<Spinner v-if="saving || resourceFormRef?.isValidating" class="w-4 h-4" />
|
|
25
|
+
<IconFloppyDiskSolid v-else class="w-4 h-4" />
|
|
25
26
|
{{ $t('Save') }}
|
|
26
27
|
</button>
|
|
27
28
|
|
|
@@ -50,7 +51,7 @@
|
|
|
50
51
|
:adminUser="coreStore.adminUser"
|
|
51
52
|
@update:record="onUpdateRecord"
|
|
52
53
|
@update:isValid="isValid = $event"
|
|
53
|
-
:
|
|
54
|
+
:validatingMode="validatingMode"
|
|
54
55
|
:source="'edit'"
|
|
55
56
|
>
|
|
56
57
|
</ResourceForm>
|
|
@@ -85,13 +86,15 @@ import { useI18n } from 'vue-i18n';
|
|
|
85
86
|
import { formatComponent } from '@/utils';
|
|
86
87
|
import { type AdminForthComponentDeclaration, type AdminForthComponentDeclarationFull } from '@/types/Common.js';
|
|
87
88
|
import type { AdminForthResourceColumn } from '@/types/Back';
|
|
89
|
+
import { scrollToInvalidField, saveRecordPreparations } from '@/utils';
|
|
90
|
+
import { Spinner } from '@/afcl'
|
|
88
91
|
|
|
89
92
|
const { t } = useI18n();
|
|
90
93
|
const coreStore = useCoreStore();
|
|
91
94
|
const { clearSaveInterceptors, runSaveInterceptors, alert, confirm } = useAdminforth();
|
|
92
95
|
|
|
93
96
|
const isValid = ref(false);
|
|
94
|
-
const
|
|
97
|
+
const validatingMode = ref(false);
|
|
95
98
|
|
|
96
99
|
const route = useRoute();
|
|
97
100
|
const router = useRouter();
|
|
@@ -180,22 +183,20 @@ onMounted(async () => {
|
|
|
180
183
|
});
|
|
181
184
|
|
|
182
185
|
async function saveRecord() {
|
|
183
|
-
if (!isValid.value) {
|
|
184
|
-
validating.value = true;
|
|
185
|
-
await nextTick();
|
|
186
|
-
scrollToInvalidField();
|
|
187
|
-
return;
|
|
188
|
-
} else {
|
|
189
|
-
validating.value = false;
|
|
190
|
-
}
|
|
191
186
|
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
187
|
+
const interceptorsResult = await saveRecordPreparations(
|
|
188
|
+
'edit',
|
|
189
|
+
validatingMode,
|
|
190
|
+
resourceFormRef,
|
|
191
|
+
isValid,
|
|
192
|
+
t,
|
|
193
|
+
saving,
|
|
194
|
+
runSaveInterceptors,
|
|
195
|
+
record,
|
|
196
|
+
coreStore,
|
|
197
|
+
route
|
|
198
|
+
);
|
|
199
|
+
|
|
199
200
|
if (!interceptorsResult.ok) {
|
|
200
201
|
saving.value = false;
|
|
201
202
|
if (interceptorsResult.error) showErrorTost(interceptorsResult.error);
|
|
@@ -256,23 +257,4 @@ async function saveRecord() {
|
|
|
256
257
|
saving.value = false;
|
|
257
258
|
}
|
|
258
259
|
|
|
259
|
-
function scrollToInvalidField() {
|
|
260
|
-
let columnsWithErrors: {column: AdminForthResourceColumn, error: string}[] = [];
|
|
261
|
-
for (const column of resourceFormRef.value?.editableColumns || []) {
|
|
262
|
-
const error = resourceFormRef.value?.columnError(column);
|
|
263
|
-
if (error) {
|
|
264
|
-
columnsWithErrors.push({column, error});
|
|
265
|
-
}
|
|
266
|
-
}
|
|
267
|
-
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>';
|
|
268
|
-
alert({
|
|
269
|
-
messageHtml: errorMessage,
|
|
270
|
-
variant: 'danger'
|
|
271
|
-
});
|
|
272
|
-
const firstInvalidElement = document.querySelector('.af-invalid-field-message');
|
|
273
|
-
if (firstInvalidElement) {
|
|
274
|
-
firstInvalidElement.scrollIntoView({ behavior: 'smooth', block: 'center' });
|
|
275
|
-
}
|
|
276
|
-
}
|
|
277
|
-
|
|
278
260
|
</script>
|
package/dist/types/Common.d.ts
CHANGED
|
@@ -294,7 +294,7 @@ export interface AdminForthComponentDeclarationFull {
|
|
|
294
294
|
[key: string]: any;
|
|
295
295
|
};
|
|
296
296
|
}
|
|
297
|
-
import { type AdminForthActionInput, type AdminForthResource } from './Back.js';
|
|
297
|
+
import { type IAdminForth, type AdminForthActionInput, type AdminForthResource } from './Back.js';
|
|
298
298
|
export { type AdminForthActionInput } from './Back.js';
|
|
299
299
|
export type AdminForthComponentDeclaration = AdminForthComponentDeclarationFull | string;
|
|
300
300
|
export type FieldGroup = {
|
|
@@ -557,13 +557,13 @@ export type ValidationObject = {
|
|
|
557
557
|
* ```
|
|
558
558
|
*
|
|
559
559
|
*/
|
|
560
|
-
regExp
|
|
560
|
+
regExp?: string;
|
|
561
561
|
/**
|
|
562
562
|
* Error message shown to user if validation fails
|
|
563
563
|
*
|
|
564
564
|
* Example: "Invalid email format"
|
|
565
565
|
*/
|
|
566
|
-
message
|
|
566
|
+
message?: string;
|
|
567
567
|
/**
|
|
568
568
|
* Whether to check case sensitivity (i flag)
|
|
569
569
|
*/
|
|
@@ -576,6 +576,25 @@ export type ValidationObject = {
|
|
|
576
576
|
* Whether to check global strings (g flag)
|
|
577
577
|
*/
|
|
578
578
|
global?: boolean;
|
|
579
|
+
/**
|
|
580
|
+
* Custom validator function.
|
|
581
|
+
*
|
|
582
|
+
* Example:
|
|
583
|
+
*
|
|
584
|
+
* ```ts
|
|
585
|
+
* validator: async (value) => {
|
|
586
|
+
* // custom validation logic
|
|
587
|
+
* return { isValid: true, message: 'Validation passed' }; // or { isValid: false, message: 'Validation failed' }
|
|
588
|
+
* }
|
|
589
|
+
* ```
|
|
590
|
+
*/
|
|
591
|
+
validator?: (value: any, record: any, adminForth: IAdminForth) => {
|
|
592
|
+
isValid: boolean;
|
|
593
|
+
message?: string;
|
|
594
|
+
} | Promise<{
|
|
595
|
+
isValid: boolean;
|
|
596
|
+
message?: string;
|
|
597
|
+
}> | boolean;
|
|
579
598
|
};
|
|
580
599
|
export declare enum AdminForthResourcePages {
|
|
581
600
|
list = "list",
|