Skip to content

Commit fb5af5f

Browse files
committed
docs: reorganise reference section into features
Move form definition formats and session cache under features/code-based, move form definition options under features, and restore page elements as a top-level feature. Add lists documentation to form definition options. Reference section now contains only the request lifecycle.
1 parent 1b246e8 commit fb5af5f

16 files changed

Lines changed: 690 additions & 329 deletions

docs/BUILDING_THE_PACKAGE.md

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,3 @@
1-
---
2-
layout: default
3-
title: Building the package
4-
render_with_liquid: false
5-
nav_order: 5
6-
---
7-
81
# Building the package
92

103
1. [Overview](#overview)

docs/contributing.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,3 +56,7 @@ If you would like to fix the bug yourself, contributions are accepted through pu
5656
### Adding features
5757

5858
Features should be discussed with the Defra Forms team prior to implementation. This is to prevent wasted effort if the Defra Forms team decides not to accept it, or if we suggest any significant amendments. Reach out to us on [#defra-forms-support](https://defra-digital-team.slack.com) to discuss your requirements. If accepted by the product owner, we welcome a pull request.
59+
60+
## Building and publishing the package
61+
62+
See the [Building the package](./BUILDING_THE_PACKAGE) guide for documentation on the build pipeline, path alias resolution, and the npm publish workflow.
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
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.

docs/features/code-based/index.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,15 @@ Automatically copy query string parameter values into hidden fields on first loa
3131
## [Save and Exit](./code-based/save-and-exit)
3232

3333
Show a secondary "Save and exit" button on question pages and handle the persisted session using a route handler you supply, enabling users to leave and resume their journey later.
34+
35+
## [Template Extensions](./code-based/template-extensions)
36+
37+
Add custom globals and filters to the Nunjucks template environment, making them available across all form page templates and LiquidJS page templates.
38+
39+
## [Form Definition Formats](./code-based/form-definition-formats)
40+
41+
Options for loading form definitions — file-based loading with the built-in `FileFormService`, or a custom `formsService` implementation for API and database sources.
42+
43+
## [Session Cache](./code-based/session-cache)
44+
45+
Configuring the server-side session store for production: named catbox cache or a custom `CacheService` subclass.
File renamed without changes.

0 commit comments

Comments
 (0)