|
| 1 | +--- |
| 2 | +title: JSON Form |
| 3 | +description: "Guide to the JSON Form plugin — render a rich, schema-driven form for a JSON column instead of a raw text area, using a JSON Schema extended with x-* layout keywords." |
| 4 | +slug: /tutorial/Plugins/json-form |
| 5 | +--- |
| 6 | + |
| 7 | +# JSON Form |
| 8 | + |
| 9 | +The JSON Form plugin renders a rich, schema-driven form for a **JSON** column instead of the default raw text area. You describe the shape of the data with a JSON Schema (extended with a few `x-*` keywords for layout and validation messages) and the form is generated by [**jedison**](https://germanbisurgi.github.io/jedison-docs/), a JSON-Schema form generator, mounted inside the AdminForth create/edit views. |
| 10 | + |
| 11 | +Unlike the [JSON Editor](/tutorial/Plugins/json-editor) plugin (a generic key-value editor), the JSON Form plugin gives you a fixed, validated form: nested objects, arrays, enums, discriminated unions, grids, tabs and typed inputs — all driven by a schema you control. |
| 12 | + |
| 13 | +## Installation |
| 14 | + |
| 15 | +```bash |
| 16 | +pnpm add @adminforth/json-form --save |
| 17 | +``` |
| 18 | + |
| 19 | +## Setting up |
| 20 | + |
| 21 | +The plugin works **only with `json` columns**. If the target column is not of type `json`, the plugin throws during config validation. |
| 22 | + |
| 23 | +First update schema: |
| 24 | + |
| 25 | +```title="./schema.prisma" |
| 26 | +model apartments { |
| 27 | + ... |
| 28 | + //diff-add |
| 29 | + config Json? |
| 30 | +} |
| 31 | +``` |
| 32 | +and make migration: |
| 33 | + |
| 34 | +```bash |
| 35 | +pnpm makemigration --name add-apartment-config; pnpm migrate:local |
| 36 | +``` |
| 37 | + |
| 38 | +Then make sure the target column is declared with the `json` datatype: |
| 39 | + |
| 40 | +```ts title="./resources/apartments.ts" |
| 41 | +import { AdminForthDataTypes } from 'adminforth'; |
| 42 | + |
| 43 | +export default { |
| 44 | + ... |
| 45 | + columns: [ |
| 46 | + ... |
| 47 | +//diff-add |
| 48 | + { |
| 49 | +//diff-add |
| 50 | + name: 'config', |
| 51 | +//diff-add |
| 52 | + type: AdminForthDataTypes.JSON, // required: must be JSON |
| 53 | +//diff-add |
| 54 | + label: 'Config', |
| 55 | +//diff-add |
| 56 | + }, |
| 57 | + ], |
| 58 | +} |
| 59 | +``` |
| 60 | + |
| 61 | +And finally import and attach the plugin, passing the field name and a schema: |
| 62 | + |
| 63 | +```ts title="./resources/apartments.ts" |
| 64 | +//diff-add |
| 65 | +import JsonFormPlugin from '@adminforth/json-form'; |
| 66 | + |
| 67 | +export default { |
| 68 | + ... |
| 69 | + plugins: [ |
| 70 | + ... |
| 71 | +//diff-add |
| 72 | + new JsonFormPlugin({ |
| 73 | +//diff-add |
| 74 | + fieldName: 'config', |
| 75 | +//diff-add |
| 76 | + schema: { |
| 77 | +//diff-add |
| 78 | + title: 'RPG Character Creator', |
| 79 | +//diff-add |
| 80 | + description: 'Create and customize your adventurer', |
| 81 | +//diff-add |
| 82 | + type: 'object', |
| 83 | +//diff-add |
| 84 | + 'x-format': 'nav-vertical', |
| 85 | +//diff-add |
| 86 | + properties: { |
| 87 | +//diff-add |
| 88 | + identity: { |
| 89 | +//diff-add |
| 90 | + title: 'Identity', |
| 91 | +//diff-add |
| 92 | + type: 'object', |
| 93 | +//diff-add |
| 94 | + 'x-format': 'grid', |
| 95 | +//diff-add |
| 96 | + required: ['name'], |
| 97 | +//diff-add |
| 98 | + properties: { |
| 99 | +//diff-add |
| 100 | + name: { |
| 101 | +//diff-add |
| 102 | + title: 'Name', |
| 103 | +//diff-add |
| 104 | + type: 'string', |
| 105 | +//diff-add |
| 106 | + minLength: 2, |
| 107 | +//diff-add |
| 108 | + 'x-grid': { columns: 6 }, |
| 109 | +//diff-add |
| 110 | + 'x-messages': { required: 'Every adventurer needs a name!' }, |
| 111 | +//diff-add |
| 112 | + default: 'Thalion Oakenshield', |
| 113 | +//diff-add |
| 114 | + }, |
| 115 | +//diff-add |
| 116 | + // ... |
| 117 | +//diff-add |
| 118 | + }, |
| 119 | +//diff-add |
| 120 | + }, |
| 121 | +//diff-add |
| 122 | + // ... |
| 123 | +//diff-add |
| 124 | + }, |
| 125 | +//diff-add |
| 126 | + }, |
| 127 | +//diff-add |
| 128 | + }), |
| 129 | + ], |
| 130 | +} |
| 131 | +``` |
| 132 | + |
| 133 | +The same form is rendered in both the create and edit views for the `config` field. |
| 134 | + |
0 commit comments