Skip to content

Commit d5d7bc9

Browse files
committed
fix: fix fillOnCreate for the isArray columns
1 parent 4000831 commit d5d7bc9

File tree

2 files changed

+10
-4
lines changed

2 files changed

+10
-4
lines changed

adminforth/dataConnectors/baseConnector.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -253,13 +253,13 @@ export default class AdminForthBaseConnector implements IAdminForthDataSourceCon
253253
resource: AdminForthResource; record: any; adminUser: any;
254254
}): Promise<{ error?: string; ok: boolean; createdRecord?: any; }> {
255255
// transform value using setFieldValue and call createRecordOriginalValues
256-
256+
257257
const filledRecord = {...record};
258258
const recordWithOriginalValues = {...record};
259259

260260
for (const col of resource.dataSourceColumns) {
261261
if (col.fillOnCreate) {
262-
if (filledRecord[col.name] === undefined) {
262+
if (filledRecord[col.name] === undefined || (Array.isArray(filledRecord[col.name]) && filledRecord[col.name].length === 0)) {
263263
filledRecord[col.name] = col.fillOnCreate({
264264
initialRecord: record,
265265
adminUser

adminforth/modules/restApi.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,11 @@ async function isShown(
6464
return true;
6565
}
6666

67+
async function isFilledOnCreate( col: AdminForthResource['columns'][number] ): Promise<boolean> {
68+
const fillOnCreate = !!col.fillOnCreate;
69+
return fillOnCreate;
70+
}
71+
6772
export async function interpretResource(
6873
adminUser: AdminUser,
6974
resource: AdminForthResource,
@@ -1190,9 +1195,10 @@ export default class AdminForthRestAPI implements IAdminForthRestAPI {
11901195
for (const column of resource.columns) {
11911196
const fieldName = column.name;
11921197
if (fieldName in record) {
1193-
const shown = await isShown(column, 'create', ctxCreate);
1198+
const shown = await isShown(column, 'create', ctxCreate); //
11941199
const bo = await isBackendOnly(column, ctxCreate);
1195-
if (!shown || bo) {
1200+
const filledOnCreate = await isFilledOnCreate(column);
1201+
if ((!shown && !filledOnCreate) || bo) {
11961202
return { error: `Field "${fieldName}" cannot be modified as it is restricted from creation (backendOnly or showIn.create is false, please set it to true)`, ok: false };
11971203
}
11981204
}

0 commit comments

Comments
 (0)