x-govuk/govuk-form-builder

Displaying all errors for child attributes

Closed this issue · 2 comments

The form's model I'm working on has a has_many association that can be updated via a text input. When entering invalid values, I'm only getting the first record's error because of this line https://github.com/DFE-Digital/govuk_design_system_formbuilder/blob/master/lib/govuk_design_system_formbuilder/elements/error_summary.rb#L39. Something that would work for me would be list_item(attribute, messages.join(', ')).

What is the reason for only getting the first error? Is that the expected behaviour?

I'm also struggling to link the error to the custom text input because the name doesn't match. I'm not sure if the link can be removed, or updated so it goes to an existing element ID.

Update:

Have discovered a link_errors option, tried it unsuccessfully on the field and on the form as it doesn't seem to be changing anything. I might be misunderstanding the purpose of that option.

<%= form_with model: @area, local: true, link_errors: false do |form| %>
  <% render_error_summary(form) %>

  <%= form.govuk_text_field :tag, value: @area.name %>
  <%= form.govuk_text_field :postcodes_attributes, class: 'tagify normalize-tagify', value: @area.postcodes, link_errors: true %>

  <%= form.submit t('form.save') %>
<% end %>

Hi, thanks for reporting.

Originally the form builder did list more than one error per field exactly as you suggest, but that was removed some time ago because the design system guidance make it clear that it should be presented in as clear a format as possible.

Regarding the custom field generated by tagify, it's currently not possible to link directly to that because as far as Rails is concerned that's not the field associated with the attribute. I imagine it's trying to link to the text field that's been hidden by JavaScript. You probably need to be careful trying to fix this via form arguments because if someone visits your page without JavaScript enabled it won't work at all; I'd probably suggest writing a Tagify callback/event/hook that, when loaded, updates the error summary hyperlink target accordingly.

Finally, yeah - link_errors is used for something else I'm afraid. It's intended to be used on radios and checkboxes to tell the builder to give it the appropriate error ID that matches the error summary. Without it, the form builder doesn't really know which input for that attribute is first - this means it'll still work if you have an if statement around your first check box.

Hi @peteryates Thank you for your quick reply.

I understand the rationale behind having one error per field, but at the same time it makes it look like only one of the multiple entries has an error, so it can be confusing to the user. The other issues I've mentioned are quite niche and I wasn't hopeful that a straightforward solution exists. At least it's a bit clearer now.

Thank you again for your help, I'll close this issue.