Skip to content

Commit edca1e3

Browse files
committed
feat: add configuration options to allow modifying hidden fields during create and edit
1 parent 7f6c4e8 commit edca1e3

File tree

4 files changed

+57
-4
lines changed

4 files changed

+57
-4
lines changed

adminforth/documentation/docs/tutorial/03-Customization/02-customFieldRendering.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,9 @@ Sometimes a custom editor needs to update not only its own field, but also other
239239
240240
For this, custom `edit`/`create` components can emit an `update:recordFieldValue` event with the payload `{ fieldName, fieldValue }`. AdminForth will update the corresponding field in the record.
241241
242+
> If you emit `update:recordFieldValue` to modify a field which is hidden by `showIn.create: false` / `showIn.edit: false`, the backend will reject the request by default.
243+
> To allow this, set the target column config to `allowModifyWhenNotShowInCreate: true` and/or `allowModifyWhenNotShowInEdit: true`.
244+
242245
```html title='./custom/TitleWithSlugEditor.vue'
243246
<template>
244247
<div class="flex flex-col gap-2">

adminforth/documentation/docs/tutorial/08-Plugins/05-1-upload-api.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -284,6 +284,9 @@ Sometimes you want to upload a file from one field (custom editor) but store the
284284
2. Upload from the browser directly to storage.
285285
3. Emit `update:recordFieldValue` from the custom component with `{ fieldName: pathColumnName, fieldValue: filePath }` so the other field is updated.
286286

287+
> Note: if the target UploadPlugin column is hidden on the current page (`showIn.create: false` / `showIn.edit: false`), the backend rejects such updates by default.
288+
> To allow this, set the target column config to `allowModifyWhenNotShowInCreate: true` and/or `allowModifyWhenNotShowInEdit: true`.
289+
287290
This lets you reuse the same Upload plugin instance (and its preview logic) while controlling the UX from a different field.
288291

289292
Example Vue custom editor that uploads an avatar and writes the result into another field handled by Upload plugin:

adminforth/modules/restApi.ts

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1233,8 +1233,18 @@ export default class AdminForthRestAPI implements IAdminForthRestAPI {
12331233
const shown = await isShown(column, 'create', ctxCreate); //
12341234
const bo = await isBackendOnly(column, ctxCreate);
12351235
const filledOnCreate = await isFilledOnCreate(column);
1236-
if ((!shown && !filledOnCreate) || bo) {
1237-
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 };
1236+
if (bo) {
1237+
return {
1238+
error: `Field "${fieldName}" cannot be modified as it is restricted from creation (backendOnly is true).`,
1239+
ok: false,
1240+
};
1241+
}
1242+
1243+
if (!shown && !filledOnCreate && !column.allowModifyWhenNotShowInCreate) {
1244+
return {
1245+
error: `Field "${fieldName}" cannot be modified as it is restricted from creation (showIn.create is false). If you need to set this hidden field during creation, either configure column.fillOnCreate or set column.allowModifyWhenNotShowInCreate = true.`,
1246+
ok: false,
1247+
};
12381248
}
12391249
}
12401250
}
@@ -1354,8 +1364,25 @@ export default class AdminForthRestAPI implements IAdminForthRestAPI {
13541364
if (fieldName in record) {
13551365
const shown = await isShown(column, 'edit', ctxEdit);
13561366
const bo = await isBackendOnly(column, ctxEdit);
1357-
if (!shown || column.editReadonly || bo) {
1358-
return { error: `Field "${fieldName}" cannot be modified as it is restricted from editing (backendOnly or showIn.edit is false, please set it to true)`, ok: false };
1367+
if (bo) {
1368+
return {
1369+
error: `Field "${fieldName}" cannot be modified as it is restricted from editing (backendOnly is true).`,
1370+
ok: false,
1371+
};
1372+
}
1373+
1374+
if (column.editReadonly) {
1375+
return {
1376+
error: `Field "${fieldName}" cannot be modified as it is restricted from editing (editReadonly is true).`,
1377+
ok: false,
1378+
};
1379+
}
1380+
1381+
if (!shown && !column.allowModifyWhenNotShowInEdit) {
1382+
return {
1383+
error: `Field "${fieldName}" cannot be modified as it is restricted from editing (showIn.edit is false). If you need to allow updating this hidden field during editing, set column.allowModifyWhenNotShowInEdit = true.`,
1384+
ok: false,
1385+
};
13591386
}
13601387
}
13611388
}

adminforth/types/Common.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -723,6 +723,26 @@ export interface AdminForthResourceColumnInputCommon {
723723
*/
724724
editReadonly?: boolean,
725725

726+
/**
727+
* Allows updating this column on Edit requests even when `showIn.edit` resolves to `false`.
728+
*
729+
* Default is `false`.
730+
*
731+
* This is useful for custom edit components which update hidden/technical fields via
732+
* `update:recordFieldValue`.
733+
*/
734+
allowModifyWhenNotShowInEdit?: boolean,
735+
736+
/**
737+
* Allows setting this column on Create requests even when `showIn.create` resolves to `false`.
738+
*
739+
* Default is `false`.
740+
*
741+
* This is useful for custom create components which update hidden/technical fields via
742+
* `update:recordFieldValue`.
743+
*/
744+
allowModifyWhenNotShowInCreate?: boolean,
745+
726746
/**
727747
* Defines on which AdminForth pages this field will be shown. By default all.
728748
* Example: if you want to show field only in create and edit pages, set it to

0 commit comments

Comments
 (0)