Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 52 additions & 0 deletions docs/features/code-based/CUSTOM_SERVICES.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,55 @@ This method is invoked for every page request.

Only when the `formMetadata` indicates that the definition has changed is a call to `getFormDefinition` is made.
The response from this can be quite big as it contains the entire form definition.

## Loading forms from files

To create a `formsService` from form config files that live on disk, you can use the `FileFormService` class.
Form definition config files can be either `.json` or `.yaml`.

Once created and files have been loaded using the `addForm` method,
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).

```javascript
import { FileFormService } from '@defra/forms-engine-plugin/file-form-service.js'

// Create shared form metadata
const now = new Date()
const user = { id: 'user', displayName: 'Username' }
const author = { createdAt: now, createdBy: user, updatedAt: now, updatedBy: user }
const metadata = {
organisation: 'Defra',
teamName: 'Team name',
teamEmail: 'team@defra.gov.uk',
submissionGuidance: "Thanks for your submission, we'll be in touch",
notificationEmail: 'email@domain.com',
...author,
live: author
}

// Instantiate the file loader form service
const loader = new FileFormService()

// Add a Json form
await loader.addForm(
'src/definitions/example-form.json', {
...metadata,
id: '95e92559-968d-44ae-8666-2b1ad3dffd31',
title: 'Example Json',
slug: 'example-json'
}
)

// Add a Yaml form
await loader.addForm(
'src/definitions/example-form.yaml', {
...metadata,
id: '641aeafd-13dd-40fa-9186-001703800efb',
title: 'Example Yaml',
slug: 'example-yaml'
}
)

// Get the forms service
const formsService = loader.toFormsService()
```
24 changes: 19 additions & 5 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
"./file-upload.min.js": "./.public/javascripts/file-upload.min.js",
"./file-upload.min.js.map": "./.public/javascripts/file-upload.min.js.map",
"./application.min.css": "./.public/stylesheets/application.min.css",
"./file-form-service.js": "./.server/server/utils/file-form-service.js",
"./controllers/*": "./.server/server/plugins/engine/pageControllers/*",
"./services/*": "./.server/server/plugins/engine/services/*",
"./package.json": "./package.json"
Expand Down Expand Up @@ -103,7 +104,8 @@
"pino": "^9.6.0",
"pino-pretty": "^13.0.0",
"proxy-agent": "^6.5.0",
"resolve": "^1.22.10"
"resolve": "^1.22.10",
"yaml": "^2.7.1"
},
"devDependencies": {
"@babel/cli": "^7.26.4",
Expand Down
Loading
Loading