Skip to content

Commit 0556941

Browse files
Update docs to include FileFormService
1 parent 3cc474f commit 0556941

1 file changed

Lines changed: 52 additions & 0 deletions

File tree

docs/features/code-based/CUSTOM_SERVICES.md

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,3 +44,55 @@ This method is invoked for every page request.
4444

4545
Only when the `formMetadata` indicates that the definition has changed is a call to `getFormDefinition` is made.
4646
The response from this can be quite big as it contains the entire form definition.
47+
48+
## Loading forms from files
49+
50+
To create a `formsService` from form config files that live on disk, you can use the `FileFormService` class.
51+
Form definition config files can be either `.json` or `.yaml`.
52+
53+
Once created and files have been loaded using the `addForm` method,
54+
call the `toFormsService` method to return a `FormService` compliant interface which can be passed in to the `services` setting of the [plugin options](/forms-engine-plugin/PLUGIN_OPTIONS.md).
55+
56+
```javascript
57+
import { FileFormService } from '@defra/forms-engine-plugin/file-form-service.js'
58+
59+
// Create shared form metadata
60+
const now = new Date()
61+
const user = { id: 'user', displayName: 'Username' }
62+
const author = { createdAt: now, createdBy: user, updatedAt: now, updatedBy: user }
63+
const metadata = {
64+
organisation: 'Defra',
65+
teamName: 'Team name',
66+
teamEmail: 'team@defra.gov.uk',
67+
submissionGuidance: "Thanks for your submission, we'll be in touch",
68+
notificationEmail: 'email@domain.com',
69+
...author,
70+
live: author
71+
}
72+
73+
// Instantiate the file loader form service
74+
const loader = new FileFormService()
75+
76+
// Add a Json form
77+
await loader.addForm(
78+
'src/definitions/example-form.json', {
79+
...metadata,
80+
id: '95e92559-968d-44ae-8666-2b1ad3dffd31',
81+
title: 'Example Json',
82+
slug: 'example-json'
83+
}
84+
)
85+
86+
// Add a Yaml form
87+
await loader.addForm(
88+
'src/definitions/example-form.json', {
89+
...metadata,
90+
id: '641aeafd-13dd-40fa-9186-001703800efb',
91+
title: 'Example Yaml',
92+
slug: 'example-yaml'
93+
}
94+
)
95+
96+
// Get the forms service
97+
const formsService = loader.toFormsService()
98+
```

0 commit comments

Comments
 (0)