Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 45 additions & 0 deletions adminforth/modules/configValidator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -400,6 +400,10 @@ export default class ConfigValidator implements IConfigValidator {

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

if (col.showIn.create && inCol.fillOnCreate !== undefined) {
errors.push(`Resource "${res.resourceId}" column "${col.name}" is present on crate page and has fillOnCreate`);
}

// check col.required is boolean or object
if (inCol.required && !((typeof inCol.required === 'boolean') || (typeof inCol.required === 'object'))) {
errors.push(`Resource "${res.resourceId}" column "${col.name}" required must be a boolean or object`);
Expand Down Expand Up @@ -459,6 +463,47 @@ export default class ConfigValidator implements IConfigValidator {
}
}
}

// check suggestOnCreate types
if (inCol.suggestOnCreate !== undefined) {
if (!col.showIn.create) {
errors.push(`Resource "${res.resourceId}" column "${col.name}" suggestOnCreate is present, while column is hidden on create page`);
}

if (inCol.suggestOnCreate === '' || inCol.suggestOnCreate === null) {
errors.push(`Resource "${res.resourceId}" column "${col.name}" suggestOnCreate must not be empty`);
}

if (!['string', 'number', 'boolean', 'object'].includes(typeof inCol.suggestOnCreate)) {
errors.push(`Resource "${res.resourceId}" column "${col.name}" suggestOnCreate must be a string, number, boolean or object`);
}

// if suggestOnCreate is string, column should be one of the types with text inputs
if (typeof inCol.suggestOnCreate === 'string' && ![AdminForthDataTypes.STRING, AdminForthDataTypes.DATE, AdminForthDataTypes.DATETIME, AdminForthDataTypes.TIME, AdminForthDataTypes.TEXT, AdminForthDataTypes.RICHTEXT, undefined].includes(inCol.type)) {
errors.push(`Resource "${res.resourceId}" column "${col.name}" suggestOnCreate value does not match type of a column`);
}

if (typeof inCol.suggestOnCreate === 'number' && ![AdminForthDataTypes.INTEGER, AdminForthDataTypes.FLOAT, AdminForthDataTypes.DECIMAL].includes(inCol.type)) {
errors.push(`Resource "${res.resourceId}" column "${col.name}" suggestOnCreate value does not match type of a column`);
}

if (typeof inCol.suggestOnCreate === 'boolean' && inCol.type !== AdminForthDataTypes.BOOLEAN) {
errors.push(`Resource "${res.resourceId}" column "${col.name}" suggestOnCreate value does not match type of a column`);
}

if (inCol.enum && !inCol.enum.map((ei) => ei.value).includes(inCol.suggestOnCreate)) {
errors.push(`Resource "${res.resourceId}" column "${col.name}" suggestOnCreate value is not in enum`);
}

if (typeof inCol.suggestOnCreate === 'object' && inCol.type !== AdminForthDataTypes.JSON) {
errors.push(`Resource "${res.resourceId}" column "${col.name}" suggestOnCreate value does not match type of a column`);
}

if (inCol.isArray?.enabled && !Array.isArray(inCol.suggestOnCreate)) {
errors.push(`Resource "${res.resourceId}" column "${col.name}" isArray is enabled but suggestOnCreate is not an array`);
}
}

if (col.foreignResource) {

if (!col.foreignResource.resourceId) {
Expand Down
19 changes: 12 additions & 7 deletions adminforth/spa/src/views/CreateView.vue
Original file line number Diff line number Diff line change
Expand Up @@ -101,12 +101,7 @@ const record = ref({});

const coreStore = useCoreStore();

const initalValues = computed(() => {
if (!route.query.values) {
return {};
}
return JSON.parse(decodeURIComponent(route.query.values));
});
const initialValues = ref({});


async function onUpdateRecord(newRecord) {
Expand All @@ -116,10 +111,20 @@ async function onUpdateRecord(newRecord) {

onMounted(async () => {
loading.value = true;
record.value = initalValues.value;
await coreStore.fetchResourceFull({
resourceId: route.params.resourceId
});
if (route.query.values) {
initialValues.value = JSON.parse(decodeURIComponent(route.query.values));
} else {
initialValues.value = (coreStore.resource?.columns || []).reduce((acc, column) => {
if (column.suggestOnCreate !== undefined) {
acc[column.name] = column.suggestOnCreate;
}
return acc;
}, {});
}
record.value = initialValues.value;
loading.value = false;
checkAcessByAllowedActions(coreStore.resourceOptions.allowedActions,'create');
initThreeDotsDropdown();
Expand Down
7 changes: 6 additions & 1 deletion adminforth/types/Common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -674,10 +674,15 @@ export interface AdminForthResourceColumnInputCommon {
showIn?: ShowInResolved,

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

/**
* Single value that will be substituted in create form. User can change it before saving the record.
*/
suggestOnCreate?: string | number | boolean | object,

/**
* Whether AdminForth will request user to enter unique value during creating or editing record.
* This option causes AdminForth to make a request to database to check if value is unique.
Expand Down
3 changes: 3 additions & 0 deletions dev-demo/resources/audit_log.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ export default {
name: "id",
primaryKey: true,
required: false,
showIn: {
create: false,
},
fillOnCreate: ({ initialRecord }: any) => uuid(),
},
{ name: "created_at", required: false },
Expand Down
6 changes: 6 additions & 0 deletions dev-demo/resources/description_image.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,17 @@ export default {
primaryKey: true,
required: false,
fillOnCreate: ({ initialRecord }: any) => uuid(),
showIn: {
create: false,
},
},
{
name: "created_at",
required: false,
fillOnCreate: ({ initialRecord }: any) => new Date().toISOString(),
showIn: {
create: false,
},
},
{ name: "resource_id", required: false },
{ name: "record_id", required: false },
Expand Down
3 changes: 3 additions & 0 deletions dev-demo/resources/translation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,9 @@ export default {
{
name: "created_at",
fillOnCreate: ({ initialRecord, adminUser }: any) => new Date().toISOString(),
showIn: {
create: false,
},
},
{ name: "uk_string", type: AdminForthDataTypes.STRING, label: "Ukrainian" },
{ name: "ar_string", type: AdminForthDataTypes.STRING, label: "Arabic" },
Expand Down