|
| 1 | +# Form definition formats |
| 2 | + |
| 3 | +Form definitions are retrieved by `forms-engine-plugin` using the `formsService` plugin registration option. The plugin calls `getFormDefinition()` on every page request, which must return a JavaScript object matching the form definition schema. |
| 4 | + |
| 5 | +Two approaches are available: |
| 6 | + |
| 7 | +- **File-based loading** — store form definitions as YAML or JSON files in your repository and use the built-in `FileFormService` |
| 8 | +- **Custom service** — implement your own `formsService` to load definitions from an API, database, or any other source |
| 9 | + |
| 10 | +## File-based loading |
| 11 | + |
| 12 | +The built-in `FileFormService` loads form definitions from disk. YAML is recommended for forms with multi-line HTML content, as it natively supports block scalars. JSON is more portable but requires manually escaping quotes and line breaks in string values. |
| 13 | + |
| 14 | +### Registering forms |
| 15 | + |
| 16 | +```js |
| 17 | +import { FileFormService } from '@defra/forms-engine-plugin/file-form-service.js' |
| 18 | + |
| 19 | +const now = new Date() |
| 20 | +const user = { id: 'user', displayName: 'Username' } |
| 21 | +const author = { createdAt: now, createdBy: user, updatedAt: now, updatedBy: user } |
| 22 | + |
| 23 | +const loader = new FileFormService() |
| 24 | + |
| 25 | +await loader.addForm('src/definitions/example-form.yaml', { |
| 26 | + id: '95e92559-968d-44ae-8666-2b1ad3dffd31', |
| 27 | + title: 'Example form', |
| 28 | + slug: 'example-form', |
| 29 | + organisation: 'Defra', |
| 30 | + teamName: 'Team name', |
| 31 | + teamEmail: 'team@defra.gov.uk', |
| 32 | + submissionGuidance: "Thanks for your submission, we'll be in touch", |
| 33 | + notificationEmail: 'team@defra.gov.uk', |
| 34 | + ...author, |
| 35 | + live: author |
| 36 | +}) |
| 37 | + |
| 38 | +const formsService = loader.toFormsService() |
| 39 | +``` |
| 40 | + |
| 41 | +Pass the resulting `formsService` as a plugin registration option: |
| 42 | + |
| 43 | +```js |
| 44 | +await server.register({ |
| 45 | + plugin, |
| 46 | + options: { |
| 47 | + services: { formsService }, |
| 48 | + // ... |
| 49 | + } |
| 50 | +}) |
| 51 | +``` |
| 52 | + |
| 53 | +Call `loader.addForm()` once per form definition file. The `slug` controls the URL path — a form with `slug: 'example-form'` is served at `/example-form/*`. |
| 54 | + |
| 55 | +### YAML vs JSON |
| 56 | + |
| 57 | +```yaml |
| 58 | +# example-form.yaml — YAML supports multi-line content natively |
| 59 | +name: "Form name" |
| 60 | +pages: |
| 61 | + - title: "Page title" |
| 62 | + components: |
| 63 | + - type: "Html" |
| 64 | + content: | |
| 65 | + <h1 class="govuk-heading-l">Heading</h1> |
| 66 | + <p class="govuk-body">Body text</p> |
| 67 | +``` |
| 68 | +
|
| 69 | +```jsonc |
| 70 | +// example-form.json — JSON requires escaped quotes and no newlines in strings |
| 71 | +{ |
| 72 | + "name": "Form name", |
| 73 | + "pages": [ |
| 74 | + { |
| 75 | + "title": "Page title", |
| 76 | + "components": [ |
| 77 | + { |
| 78 | + "type": "Html", |
| 79 | + "content": "<h1 class=\"govuk-heading-l\">Heading</h1><p class=\"govuk-body\">Body text</p>" |
| 80 | + } |
| 81 | + ] |
| 82 | + } |
| 83 | + ] |
| 84 | +} |
| 85 | +``` |
| 86 | + |
| 87 | +## Custom formsService |
| 88 | + |
| 89 | +To load form definitions from an API, database, or CMS, implement a custom `formsService` and pass it at plugin registration. The interface requires four methods: |
| 90 | + |
| 91 | +```ts |
| 92 | +interface FormsService { |
| 93 | + getFormMetadata: (slug: string) => Promise<FormMetadata> |
| 94 | + getFormMetadataById: (id: string) => Promise<FormMetadata> |
| 95 | + getFormDefinition: (id: string, state: FormStatus) => Promise<FormDefinition | undefined> |
| 96 | + getFormSecret: (formId: string, secretName: string) => Promise<string> |
| 97 | +} |
| 98 | +``` |
| 99 | + |
| 100 | +`getFormMetadata` is called on every page request and should be fast. `getFormDefinition` is only called when the metadata signals the definition has changed, so it can do heavier lifting. |
| 101 | + |
| 102 | +See [Custom Services](./custom-services) for a full implementation guide. |
0 commit comments