x-govuk/govuk-form-builder

Conditional revealed question values

Closed this issue · 4 comments

Form builder supports conditionally questions, nested under radio items and show or hides them based on the selected value. It doesn't seem like it clears the value of the conditionally hidden question on hide though. Should it?

E.g.:

  1. image
  2. image
  3. image

This is an interesting question, thank you for raising it. I don't believe it could be achieved in the form builder itself as it has no idea about which fields are conditional and doesn't come with any accompanying JavaScript. So long as the inner text field isn't disabled the value will be submitted.

The conditional reveal in govuk-frontend just hides the conditional <div> by toggling govuk-radios__conditional--hidden, here.

I think the safest approach is to check the (probably transient) submitted value for the radios in the controller and then select/omit the revealed text field attributes based on that.

If you have a lot of pages like this perhaps via naming convention it can be streamlined.

I'll mention this issue on DfE's Slack, I'm sure people have encountered and dealt with it before.

I think the safest approach is to check the (probably transient) submitted value for the radios in the controller and then select/omit the revealed text field attributes based on that.

This is what we’ve done in Find a lost TRN: https://github.com/DFE-Digital/find-a-lost-trn/blob/main/app/forms/name_form.rb#L32

The form looks like this:

4AFB8D4B-4BC7-42BA-BBB4-A033822BF42D

You can write anything in the previous fields; if you deselect the checkbox and submit, the previous first name and previous last names are set to nil, regardless of what’s in the actual fields.

You can play around with it here (try to progress until the Check answers page, and then try different combinations of names and previous names) https://dev-find-a-lost-trn.education.gov.uk/name

There is some helpful validation, like you can’t tick the “I’ve changed my name” checkbox without entering at least one previous name: https://github.com/DFE-Digital/find-a-lost-trn/blob/main/app/forms/name_form.rb#L11 (first time I've used the absence validation)

We're currently working around this with custom JS (https://github.com/communitiesuk/submit-social-housing-lettings-and-sales-data/blob/main/app/frontend/controllers/conditional_question_controller.js) to clear the field (backend works similarly to your example).

Going to close this. I think it's outside of the form builder's remit. Happy to revisit if there's anything we can do to help.