GithubHelp home page GithubHelp logo

Comments (6)

peteryates avatar peteryates commented on September 13, 2024

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.

peteryates avatar peteryates commented on September 13, 2024

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.

sfnelson avatar sfnelson commented on September 13, 2024

@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.

sfnelson avatar sfnelson commented on September 13, 2024

... 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.

peteryates avatar peteryates commented on September 13, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.