You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+82-11Lines changed: 82 additions & 11 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -36,14 +36,74 @@ Optional dependencies
36
36
37
37
### Templates and views
38
38
39
-
Vision and nunjucks both need to be configured to search in the forms plugin module directory when looking for views.
40
-
41
-
The path is `node_modules/@defra/forms-engine/src/server/plugins/engine/views`.
39
+
Vision and nunjucks both need to be configured to search in the `forms-engine-plugin` views directory when looking for template files.
42
40
43
41
For vision this is done through the `path`[plugin option](https://github.com/hapijs/vision/blob/master/API.md#options)
44
-
For nunjucks it is configured through the environment [confgure options](https://mozilla.github.io/nunjucks/api.html#configure).
42
+
For nunjucks it is configured through the environment [configure options](https://mozilla.github.io/nunjucks/api.html#configure).
43
+
44
+
The `forms-engine-plugin` path to add can be imported from:
45
+
46
+
`import { VIEW_PATH } from '@defra/forms-engine-plugin'`
47
+
48
+
Which can then be appended to the `node_modules` path `node_modules/@defra/forms-engine`.
49
+
50
+
The main template layout is `govuk-frontend`'s `template.njk` file, this also needs to be added to the `path`s that nunjucks can look in.
51
+
52
+
See example below for more detail.
53
+
54
+
### Form config
55
+
56
+
The `form-engine-plugin` uses JSON configuration files to serve form journeys.
57
+
These files are called `Form definitions` and are built up of:
58
+
59
+
-`pages` - includes a `path`, `title`
60
+
-`components` - one or more questions on a page
61
+
-`conditions` - used to conditionally show and hide pages and
62
+
-`lists` - data used to in selection fields like [Select](https://design-system.service.gov.uk/components/select/), [Checkboxes](https://design-system.service.gov.uk/components/checkboxes/) and [Radios](https://design-system.service.gov.uk/components/radios/)
63
+
64
+
The [types](https://github.com/DEFRA/forms-designer/blob/main/model/src/form/form-definition/types.ts), `joi`[schema](https://github.com/DEFRA/forms-designer/blob/main/model/src/form/form-definition/index.ts) and the [examples](test/form/definitions) folder are a good place to learn about the structure of these files.
65
+
66
+
TODO - Link to wiki for `Form metadata`
67
+
TODO - Link to wiki for `Form definition`
68
+
69
+
#### Providing form config to the engine
70
+
71
+
The engine plugin registers several [routes](https://hapi.dev/tutorials/routing/?lang=en_US) on the hapi server.
72
+
73
+
They look like this:
74
+
75
+
```
76
+
GET /{slug}/{path}
77
+
POST /{slug}/{path}
78
+
```
79
+
80
+
A unique `slug` is used to route the user to the correct form, and the `path` used to identify the correct page within the form to show.
81
+
The [plugin registration options](#options) have a `services` setting to provide a `formsService` that is responsible for returning `form definition` data.
82
+
45
83
46
-
The main template layout you use will likely be the govuk-frontend `template.njk` file, so this also needs to be added to the `path`s that nunjucks can look in.
84
+
WARNING: This below is subject to change
85
+
86
+
A `formsService` has two methods, one for returning `formMetadata` and another to return `formDefinition`s.
87
+
88
+
```
89
+
const formsService = {
90
+
getFormMetadata: async function (slug) {
91
+
// Returns the metadata for the slug
92
+
},
93
+
getFormDefinition: async function (id, state) {
94
+
// Returns the form definition for the given id
95
+
}
96
+
}
97
+
```
98
+
99
+
The reason for the two separate methods is caching.
100
+
`formMetadata` is a lightweight record designed to give top level information about a form.
101
+
This method is invoked for every page request.
102
+
103
+
Only when the `formMetadata` indicates that the definition has changed is a call to `getFormDefinition` is made.
104
+
The response from this can be quite big as it contains the entire form definition.
105
+
106
+
See [example](#example) below for more detail
47
107
48
108
### Static assets and styles
49
109
@@ -78,6 +138,8 @@ await server.register({
78
138
}
79
139
})
80
140
141
+
// Paths array to tell `vision` and `nunjucks` where template files are stored.
142
+
// This can include `server/views`, or change it to where your local template are stored.
// Nunjucks also needs an additional path configuration
162
+
// to use the templates and macros from `govuk-frontend`
99
163
const environment = nunjucks.configure(
100
164
[
101
165
...path,
102
166
'node_modules/govuk-frontend/dist'
103
167
]
104
168
)
105
169
170
+
// Applies custom filters and globals for nunjucks
171
+
// that are required by the `forms-engine-plugin`
106
172
prepareNunjucksEnvironment(environment)
107
173
108
174
options.compileOptions.environment = environment
@@ -116,6 +182,7 @@ await server.register({
116
182
}
117
183
})
118
184
185
+
// Registers the `forms-engine-plugin`
119
186
await server.register({
120
187
plugin
121
188
})
@@ -129,13 +196,17 @@ await server.start()
129
196
130
197
The forms plugin is configured with [registration options](https://hapi.dev/api/?v=21.4.0#plugins)
131
198
132
-
-`model` (optional) -
133
-
-`services` (optional) -
134
-
-`controllers` (optional) -
135
-
-`cacheName` (optional) -
199
+
-`services` (optional) - object containing `formsService`, `formSubmissionService` and `outputService`
200
+
-`formsService` - used to load `formMetadata` and `formDefinition`
201
+
-`formSubmissionService` - used prepare the form during submission (ignore - subject to change)
202
+
-`outputService` - used to save the submission
203
+
-`controllers` (optional) - Object map of custom page controllers used to override the default. See [custom controllers](#custom-controllers)
204
+
-`cacheName` (optional) - The cache name to use. Defaults to hapi's [default server cache]. Recommended for production. See [here](#custom-cache) for more details
136
205
137
206
### Services
138
207
208
+
TODO
209
+
139
210
### Custom controllers
140
211
141
212
TODO
@@ -167,14 +238,14 @@ const server = new Hapi.Server({
167
238
168
239
## Exemplar
169
240
170
-
TODO: Link to exemplar
241
+
TODO: Link to CDP exemplar
171
242
172
243
## Templates
173
244
174
245
The following elements support [LiquidJS templates](https://liquidjs.com/):
175
246
176
247
- Page **title**
177
-
- Form component **titles**
248
+
- Form component **title**
178
249
- Support for fieldset legend text or label text
179
250
- This includes when the title is used in **error messages**
0 commit comments