Skip to content

Commit fa9f6bd

Browse files
committed
Handles missing payment via PaymentField state validation
1 parent b271e1a commit fa9f6bd

3 files changed

Lines changed: 18 additions & 20 deletions

File tree

src/server/forms/payment-test.yaml

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,17 @@ schema: 2
33
name: Payment Test Form
44
declaration: "<p class=\"govuk-body\">All the answers you have provided are true to the best of your knowledge.</p>"
55
pages:
6+
- title: What is your name?
7+
path: '/person'
8+
components:
9+
- name: personName
10+
title: What is your name?
11+
type: TextField
12+
shortDescription: Your name
13+
options:
14+
required: true
15+
next:
16+
- path: '/pay-for-your-licence'
617
- title: A page title
718
path: '/pay-for-your-licence'
819
components:
@@ -28,4 +39,4 @@ pages:
2839
conditions: []
2940
sections: []
3041
lists: []
31-
startPage: '/pay-for-your-licence'
42+
startPage: '/person'

src/server/plugins/engine/components/PaymentField.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,10 @@ export class PaymentField extends FormComponent {
6868
.label(this.label)
6969

7070
this.formSchema = paymentStateSchema
71-
this.stateSchema = paymentStateSchema.default(null).allow(null)
71+
// 'required()' forces the payment page to be invalid until we have valid payment state
72+
// i.e. the user will automatically be directed back to the payment page
73+
// if they attempt to access future pages wen no payment entered yet
74+
this.stateSchema = paymentStateSchema.required()
7275
}
7376

7477
/**

src/server/plugins/engine/models/FormModel.ts

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ import { Parser, type Value } from 'expr-eval-fork'
2929
import joi from 'joi'
3030

3131
import { createLogger } from '~/src/server/common/helpers/logging/logger.js'
32-
import { type ComponentCollection } from '~/src/server/plugins/engine/components/ComponentCollection.js'
3332
import { type ListFormComponent } from '~/src/server/plugins/engine/components/ListFormComponent.js'
3433
import {} from '~/src/server/plugins/engine/components/YesNoField.js'
3534
import {
@@ -521,30 +520,15 @@ export class FormModel {
521520
}
522521
}
523522

524-
private isUnpaidPayment(
525-
context: FormContext,
526-
collection: ComponentCollection
527-
) {
528-
const paymentField = collection.fields.find(
529-
(field) => field.type === ComponentType.PaymentField
530-
)
531-
if (paymentField) {
532-
const fieldVal = paymentField.getFormValueFromState(context.state)
533-
return fieldVal === undefined
534-
}
535-
return false
536-
}
537-
538523
private assignPaths(context: FormContext) {
539-
for (const { keys, path, collection } of context.relevantPages) {
524+
for (const { keys, path } of context.relevantPages) {
540525
context.paths.push(path)
541526

542527
// Stop at page with errors
543528
if (
544529
context.errors?.some(({ name, path }) => {
545530
return keys.includes(name) || keys.some((key) => path.includes(key))
546-
}) ||
547-
this.isUnpaidPayment(context, collection)
531+
})
548532
) {
549533
break
550534
}

0 commit comments

Comments
 (0)