Skip to content

Commit 5d4b2c8

Browse files
committed
Merge branch 'next' of github.com:devforth/adminforth into next
2 parents be1c571 + 06208b8 commit 5d4b2c8

File tree

6 files changed

+77
-9
lines changed

6 files changed

+77
-9
lines changed

adminforth/modules/configValidator.ts

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -400,6 +400,10 @@ export default class ConfigValidator implements IConfigValidator {
400400

401401
col.showIn = this.validateAndNormalizeShowIn(resInput, inCol, errors, warnings);
402402

403+
if (col.showIn.create && inCol.fillOnCreate !== undefined) {
404+
errors.push(`Resource "${res.resourceId}" column "${col.name}" is present on crate page and has fillOnCreate`);
405+
}
406+
403407
// check col.required is boolean or object
404408
if (inCol.required && !((typeof inCol.required === 'boolean') || (typeof inCol.required === 'object'))) {
405409
errors.push(`Resource "${res.resourceId}" column "${col.name}" required must be a boolean or object`);
@@ -459,6 +463,47 @@ export default class ConfigValidator implements IConfigValidator {
459463
}
460464
}
461465
}
466+
467+
// check suggestOnCreate types
468+
if (inCol.suggestOnCreate !== undefined) {
469+
if (!col.showIn.create) {
470+
errors.push(`Resource "${res.resourceId}" column "${col.name}" suggestOnCreate is present, while column is hidden on create page`);
471+
}
472+
473+
if (inCol.suggestOnCreate === '' || inCol.suggestOnCreate === null) {
474+
errors.push(`Resource "${res.resourceId}" column "${col.name}" suggestOnCreate must not be empty`);
475+
}
476+
477+
if (!['string', 'number', 'boolean', 'object'].includes(typeof inCol.suggestOnCreate)) {
478+
errors.push(`Resource "${res.resourceId}" column "${col.name}" suggestOnCreate must be a string, number, boolean or object`);
479+
}
480+
481+
// if suggestOnCreate is string, column should be one of the types with text inputs
482+
if (typeof inCol.suggestOnCreate === 'string' && ![AdminForthDataTypes.STRING, AdminForthDataTypes.DATE, AdminForthDataTypes.DATETIME, AdminForthDataTypes.TIME, AdminForthDataTypes.TEXT, AdminForthDataTypes.RICHTEXT, undefined].includes(inCol.type)) {
483+
errors.push(`Resource "${res.resourceId}" column "${col.name}" suggestOnCreate value does not match type of a column`);
484+
}
485+
486+
if (typeof inCol.suggestOnCreate === 'number' && ![AdminForthDataTypes.INTEGER, AdminForthDataTypes.FLOAT, AdminForthDataTypes.DECIMAL].includes(inCol.type)) {
487+
errors.push(`Resource "${res.resourceId}" column "${col.name}" suggestOnCreate value does not match type of a column`);
488+
}
489+
490+
if (typeof inCol.suggestOnCreate === 'boolean' && inCol.type !== AdminForthDataTypes.BOOLEAN) {
491+
errors.push(`Resource "${res.resourceId}" column "${col.name}" suggestOnCreate value does not match type of a column`);
492+
}
493+
494+
if (inCol.enum && !inCol.enum.map((ei) => ei.value).includes(inCol.suggestOnCreate)) {
495+
errors.push(`Resource "${res.resourceId}" column "${col.name}" suggestOnCreate value is not in enum`);
496+
}
497+
498+
if (typeof inCol.suggestOnCreate === 'object' && inCol.type !== AdminForthDataTypes.JSON) {
499+
errors.push(`Resource "${res.resourceId}" column "${col.name}" suggestOnCreate value does not match type of a column`);
500+
}
501+
502+
if (inCol.isArray?.enabled && !Array.isArray(inCol.suggestOnCreate)) {
503+
errors.push(`Resource "${res.resourceId}" column "${col.name}" isArray is enabled but suggestOnCreate is not an array`);
504+
}
505+
}
506+
462507
if (col.foreignResource) {
463508

464509
if (!col.foreignResource.resourceId) {

adminforth/spa/src/views/CreateView.vue

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -102,12 +102,8 @@ const record = ref({});
102102
const coreStore = useCoreStore();
103103
104104
const { t } = useI18n();
105-
const initalValues = computed(() => {
106-
if (!route.query.values) {
107-
return {};
108-
}
109-
return JSON.parse(decodeURIComponent(route.query.values));
110-
});
105+
106+
const initialValues = ref({});
111107
112108
113109
async function onUpdateRecord(newRecord) {
@@ -117,10 +113,20 @@ async function onUpdateRecord(newRecord) {
117113
118114
onMounted(async () => {
119115
loading.value = true;
120-
record.value = initalValues.value;
121116
await coreStore.fetchResourceFull({
122117
resourceId: route.params.resourceId
123118
});
119+
if (route.query.values) {
120+
initialValues.value = JSON.parse(decodeURIComponent(route.query.values));
121+
} else {
122+
initialValues.value = (coreStore.resource?.columns || []).reduce((acc, column) => {
123+
if (column.suggestOnCreate !== undefined) {
124+
acc[column.name] = column.suggestOnCreate;
125+
}
126+
return acc;
127+
}, {});
128+
}
129+
record.value = initialValues.value;
124130
loading.value = false;
125131
checkAcessByAllowedActions(coreStore.resourceOptions.allowedActions,'create');
126132
initThreeDotsDropdown();

adminforth/types/Common.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -674,10 +674,15 @@ export interface AdminForthResourceColumnInputCommon {
674674
showIn?: ShowInResolved,
675675

676676
/**
677-
* Whether AdminForth will show this field in show view.
677+
* Called on the backend when the record is saved to a database. Value returned by `fillOnCreate` will be saved to the database.
678678
*/
679679
fillOnCreate?: Function,
680680

681+
/**
682+
* Single value that will be substituted in create form. User can change it before saving the record.
683+
*/
684+
suggestOnCreate?: string | number | boolean | object,
685+
681686
/**
682687
* Whether AdminForth will request user to enter unique value during creating or editing record.
683688
* This option causes AdminForth to make a request to database to check if value is unique.
@@ -792,7 +797,7 @@ export interface AdminForthResourceColumnInputCommon {
792797
*/
793798
debounceTimeMs?: number,
794799
/**
795-
* If true - will force EQ operator for filter instead of ILIKE.
800+
* If false - will force EQ operator for filter instead of ILIKE.
796801
*/
797802
substringSearch?: boolean,
798803
},

dev-demo/resources/audit_log.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ export default {
1010
name: "id",
1111
primaryKey: true,
1212
required: false,
13+
showIn: {
14+
create: false,
15+
},
1316
fillOnCreate: ({ initialRecord }: any) => uuid(),
1417
},
1518
{ name: "created_at", required: false },

dev-demo/resources/description_image.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,17 @@ export default {
1313
primaryKey: true,
1414
required: false,
1515
fillOnCreate: ({ initialRecord }: any) => uuid(),
16+
showIn: {
17+
create: false,
18+
},
1619
},
1720
{
1821
name: "created_at",
1922
required: false,
2023
fillOnCreate: ({ initialRecord }: any) => new Date().toISOString(),
24+
showIn: {
25+
create: false,
26+
},
2127
},
2228
{ name: "resource_id", required: false },
2329
{ name: "record_id", required: false },

dev-demo/resources/translation.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,9 @@ export default {
8787
{
8888
name: "created_at",
8989
fillOnCreate: ({ initialRecord, adminUser }: any) => new Date().toISOString(),
90+
showIn: {
91+
create: false,
92+
},
9093
},
9194
{ name: "uk_string", type: AdminForthDataTypes.STRING, label: "Ukrainian" },
9295
{ name: "ar_string", type: AdminForthDataTypes.STRING, label: "Arabic" },

0 commit comments

Comments
 (0)