Comments (6)
Thank you for reporting this @sfnelson. As it's a problem that'd manifest in the same way on a vanilla Rails app I think we might be best just adding a cautionary paragraph to the guide. It feels like suggesting the disabling of the setting and requiring validations to be explicitly added is the way to go.
from govuk-formbuilder.
Thinking about it, this problem will also happen in #govuk_collection_radio_buttons
. Maybe the suggestion should go in the setup guide or README.
from govuk-formbuilder.
@peteryates thanks for the reply. That seems like a pragmatic approach at present. It would be good to have some guidance for users in how to resolve this disagreement between ActiveRecord's needs and ActiveView's needs – personally, I would to see the example at https://govuk-form-builder.netlify.app/form-elements/select/#select-field-with-a-label-and-hint to use an ActiveRecord association similar to https://guides.rubyonrails.org/v7.0/form_helpers.html#the-collection-select-helper
It seems like this is the way Rails has implemented belongs_to
since at least Rails 4, so it's unlikely they are going to accept a change (e.g. recently proposed and closed rails/rails#46839). Would you consider a PR that uses reflection to identify associations and merges errors from association
and association_id
? This could be useful for more than just presence validations. Another approach to consider is rails/rails#46564
from govuk-formbuilder.
... As it's a problem that'd manifest in the same way on a vanilla Rails app ...
Vanilla Rails doesn't provide helpers for showing errors next to fields so the problem is not equivalent, e.g. this might be typical for a vanilla rails form:
<%= f.label :department_id %>
<% f.object.errors.messages_for(:department) do |message| %>
<%= tag.div message class: "error" %>
<% end %>
<%= f.collection_select :department_id, departments, :id, :name %>
As an aside, there's a rubocop to flag and remove presence validations on belongs_to associations: https://docs.rubocop.org/rubocop-rails/cops_rails.html#railsredundantpresencevalidationonbelongsto
from govuk-formbuilder.
Yeah, sorry I meant the underlying problem of the automatic validation being applied to the relationship but the form having to refer to the attribute name.
Would you consider a PR that uses reflection to identify associations and merges errors from association and association_id?
I think it could work but I suspect there's a fair bit of complexity involved. It might make sense for it to be implemented via an error summary presenter as that would allow people to opt in.
I'd still probably go with defining the validations explicitly, I'll create a PR with some amendments to the guide/README in the meantime.
from govuk-formbuilder.
Related Issues (20)
- Date Handling Errors HOT 6
- Displaying all errors for child attributes HOT 2
- Not able to check a govuk_radio_button value HOT 5
- govuk_submit should output a button instead of an input HOT 5
- Use input=date HOT 2
- It's not possible to make radio labels a regular font weight when a hint is provided HOT 1
- Setting a JSON object as an HTML attribute on form elements doesn't work HOT 1
- Idea: let hint text for date fields contain dynamic dates HOT 1
- HTML safe markup is ignored even when providing a custom presenter HOT 1
- Change hint element to div instead of span tag HOT 1
- Remove `pattern` attribute from date field
- Conditional revealed question values HOT 4
- Support hint for fieldset HOT 3
- Labels in radio button collections that include hint text are unexpectedly emboldened
- Possible improvement: localisation of nested attributes HOT 6
- Hrefs on `.govuk-error-summary__list` item links do not match IDs on `.govuk-error-message` elements HOT 2
- Can we release a patch version that supports the latest version of Pagy?
- Version 4.0.0
- Support inverse button style HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from govuk-formbuilder.