Skip to content

Commit

Permalink
Merge pull request #468 from x-govuk/add-warnings-on-belongs-to-prese…
Browse files Browse the repository at this point in the history
…nce-field-mismatch

Add warnings on `belongs_to` presence validation field mismatch
  • Loading branch information
peteryates authored Feb 16, 2024
2 parents dfd05b7 + cf28df8 commit b6f12b3
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 0 deletions.
1 change: 1 addition & 0 deletions govuk_design_system_formbuilder.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,5 @@ Gem::Specification.new do |s|
s.add_development_dependency("slim", "~> 5.2.0")
s.add_development_dependency("slim_lint", "~> 0.26.0")
s.add_development_dependency("webrick", "~> 1.8.1")
s.add_development_dependency("redcarpet", "~> 3.6.0")
end
2 changes: 2 additions & 0 deletions guide/content/form-elements/checkboxes.slim
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ p.govuk-body
hidden field can be toggled using the <code>multiple</code> keyword
argument.

== render('/partials/belongs-to-warning.*')

== render('/partials/example.*',
caption: 'Generating a collection of checkboxes',
code: checkbox_field,
Expand Down
2 changes: 2 additions & 0 deletions guide/content/form-elements/radios.slim
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ p.govuk-body
practice as it means that users of screen readers will only hear the
contents once.

== render('/partials/belongs-to-warning.*')

== render('/partials/example.*',
caption: 'Radios collection with legend and hint text',
code: radio_field_with_legend_and_hint,
Expand Down
2 changes: 2 additions & 0 deletions guide/content/form-elements/select.slim
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ p.govuk-body
| Asking questions means you're less likely to need to use the select
component and can consider using a different solution, such as radios.

== render('/partials/belongs-to-warning.*')

== render('/partials/example.*',
caption: 'Select field with a label and hint',
code: select_field_with_label_and_hint,
Expand Down
20 changes: 20 additions & 0 deletions guide/layouts/partials/belongs-to-warning.slim
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
.govuk-notification-banner[
role="region"
aria-labelledby="govuk-notification-banner-title"
data-module="govuk-notification-banner"
]
.govuk-notification-banner__header
.govuk-notification-banner__title
| Important
.govuk-notification-banner__content
markdown:
Note that Rails now adds presence validation to associations
automatically but usually we set relationships by assigning values to the
foreign key column.

This results in errors being added to the object on attributes that don't
appear in the form, for example on `department` instead of
`department_id`.

You can suppress this behaviour by adding `optional: true` to the relationship
and manually adding the presence validation to the foreign key field yourself.

0 comments on commit b6f12b3

Please sign in to comment.