diff --git a/src/server/common/forms/definitions/example-grant-with-auth.yaml b/src/server/common/forms/definitions/example-grant-with-auth.yaml index 745674a38..07b106753 100644 --- a/src/server/common/forms/definitions/example-grant-with-auth.yaml +++ b/src/server/common/forms/definitions/example-grant-with-auth.yaml @@ -394,10 +394,6 @@ pages: id: 88d7b067-68c2-4fe1-9a9a-ea98f0752e03 - title: Select all the eligible land parcels for the location of your woodland controller: CommonSelectLandParcelPageController - components: - - name: landParcels - type: CheckboxesField - title: Select land parcels config: enableMultipleParcelSelect: true topSection: | diff --git a/src/server/common/forms/definitions/example-grant-with-task-list.yaml b/src/server/common/forms/definitions/example-grant-with-task-list.yaml index 4b8396b59..af09141c2 100644 --- a/src/server/common/forms/definitions/example-grant-with-task-list.yaml +++ b/src/server/common/forms/definitions/example-grant-with-task-list.yaml @@ -236,10 +236,6 @@ pages: - title: Select land parcel for actions controller: CommonSelectLandParcelPageController - components: - - name: landParcels - type: CheckboxesField - title: Select land parcels config: enableMultipleParcelSelect: false bottomSection: | diff --git a/src/server/land-grants/common/common-select-parcel/common-select-land-parcel-page.controller.js b/src/server/land-grants/common/common-select-parcel/common-select-land-parcel-page.controller.js index 3147aac45..197c225fb 100644 --- a/src/server/land-grants/common/common-select-parcel/common-select-land-parcel-page.controller.js +++ b/src/server/land-grants/common/common-select-parcel/common-select-land-parcel-page.controller.js @@ -3,6 +3,7 @@ import LandGrantsQuestionWithAuthCheckController from '../../controllers/auth/la import { fetchParcels } from '../../services/land-grants.service.js' import { mapParcelsToViewModel } from '../../view-models/parcel.view-model.js' import { getParcelIdFromQuery, getParcelIdsFromPayload } from '../../utils/parcel-request.utils.js' +import { ComponentType } from '@defra/forms-model' export default class CommonSelectLandParcelPageController extends LandGrantsQuestionWithAuthCheckController { viewName = 'common-select-land-parcel' @@ -14,12 +15,37 @@ export default class CommonSelectLandParcelPageController extends LandGrantsQues * config: * enableMultipleParcelSelect: true * + * Ensures a `landParcels` CheckboxesField component exists on the page. + * If not defined in YAML, it is injected here so the form schema and state + * are consistent across all journeys. + * * @param {FormModel} model - * @param {import('@defra/forms-model').Page} pageDef + * @param {PageQuestion} pageDef */ constructor(model, pageDef) { - super(model, pageDef) const config = model.def.metadata?.pageConfig?.[pageDef.path] ?? {} + + const existing = pageDef.components?.find((c) => c.name === 'landParcels') + /** @type {import('@defra/forms-model').PageQuestion} */ + const patchedPageDef = { + ...pageDef, + components: existing + ? pageDef.components + : [ + ...(pageDef.components ?? []), + { + type: ComponentType.CheckboxesField, + name: 'landParcels', + title: 'Select land parcels', + list: 'landParcels', + options: { + required: true + } + } + ] + } + super(model, patchedPageDef) + this.enableMultipleParcelSelect = config.enableMultipleParcelSelect === true this.topSection = config.topSection || '' this.bottomSection = config.bottomSection || '' @@ -141,6 +167,7 @@ export default class CommonSelectLandParcelPageController extends LandGrantsQues /** * @import { FormContext, AnyFormRequest } from '@defra/forms-engine-plugin/engine/types.js' + * @import { PageQuestion } from '@defra/forms-model' * @import { FormModel } from '@defra/forms-engine-plugin/engine/models/index.js' - * @import { ResponseObject, ResponseToolkit } from '@hapi/hapi' + * @import { ResponseToolkit } from '@hapi/hapi' */ diff --git a/src/server/land-grants/common/common-select-parcel/common-select-land-parcel-page.controller.test.js b/src/server/land-grants/common/common-select-parcel/common-select-land-parcel-page.controller.test.js index 45b53154a..944d8995b 100644 --- a/src/server/land-grants/common/common-select-parcel/common-select-land-parcel-page.controller.test.js +++ b/src/server/land-grants/common/common-select-parcel/common-select-land-parcel-page.controller.test.js @@ -68,6 +68,32 @@ describe('CommonSelectLandParcelPageController', () => { afterEach(vi.clearAllMocks) + describe('constructor', () => { + it('does NOT inject CheckboxesField if landParcels already exists', () => { + const model = { + def: { metadata: { pageConfig: {} } } + } + + const existingComponent = { + type: 'CheckboxesField', + name: 'landParcels', + title: 'Existing' + } + + const pageDef = { + path: '/test', + components: [existingComponent] + } + + const controller = new CommonSelectLandParcelPageController(model, pageDef) + + const components = + controller.form?.definition?.pages?.[0]?.components || controller.pageDef?.components || pageDef.components + + expect(components).toEqual([existingComponent]) + }) + }) + describe('resolveParcelIds', () => { it('returns payload values for POST', () => { const controller = createController()