From cf22192a388db01ad9498e0dfcaf6bcec236631f Mon Sep 17 00:00:00 2001 From: jiko797torayo Date: Sat, 21 Jun 2025 15:31:59 +0900 Subject: [PATCH] Supporting form_with in Rails 8.0 --- .github/workflows/main.yml | 7 +++++++ lib/html5_validators/action_view/form_helpers.rb | 11 ++++++++++- test/fake_app.rb | 13 +++++++++++++ test/features/without_model_test.rb | 14 ++++++++++++++ 4 files changed, 44 insertions(+), 1 deletion(-) create mode 100644 test/features/without_model_test.rb diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index df84c36..8ffc254 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -10,6 +10,13 @@ jobs: rails_version: [edge, '8.0', '7.2', '7.1', '7.0', '6.1'] include: + - ruby_version: '3.2' + activemodel_version: '8.0' + + - ruby_version: '3.1' + activemodel_version: '7.2' + - ruby_version: '3.1' + activemodel_version: '7.1' - ruby_version: '3.1' activemodel_version: '7.0' diff --git a/lib/html5_validators/action_view/form_helpers.rb b/lib/html5_validators/action_view/form_helpers.rb index 952c048..1662051 100644 --- a/lib/html5_validators/action_view/form_helpers.rb +++ b/lib/html5_validators/action_view/form_helpers.rb @@ -12,7 +12,16 @@ def form_for(record, options = {}, &block) super end - if Rails::VERSION::STRING >= '5.1' + if Rails::VERSION::STRING >= '7.2' + def form_with(model: false, scope: nil, url: nil, format: nil, **options) + if model && model.respond_to?(:auto_html5_validation=) + if !Html5Validators.enabled || (options[:auto_html5_validation] == false) + model.auto_html5_validation = false + end + end + super + end + elsif Rails::VERSION::STRING >= '5.1' def form_with(model: nil, scope: nil, url: nil, format: nil, **options) if model && model.respond_to?(:auto_html5_validation=) if !Html5Validators.enabled || (options[:auto_html5_validation] == false) diff --git a/test/fake_app.rb b/test/fake_app.rb index d1097e2..b94c697 100644 --- a/test/fake_app.rb +++ b/test/fake_app.rb @@ -32,6 +32,10 @@ class Html5ValidatorsTestApp < Rails::Application get :new_with_required_false end end + + if Rails::VERSION::STRING >= '5.1' + resources :search, only: [:index] + end end # models @@ -213,6 +217,15 @@ def new_with_required_false ERB end end +class SearchController < ApplicationController + def index + render inline: <<-ERB +<%= form_with url: '/search', id: 'form_with' do |f| %> +<%= f.search_field :query, id: 'query' %> +<% end %> + ERB + end +end # helpers module ApplicationHelper; end diff --git a/test/features/without_model_test.rb b/test/features/without_model_test.rb new file mode 100644 index 0000000..a02151a --- /dev/null +++ b/test/features/without_model_test.rb @@ -0,0 +1,14 @@ +# frozen_string_literal: true + +require 'test_helper' + +class WithoutModelTest < ActionDispatch::IntegrationTest + if Rails::VERSION::STRING >= '5.1' + sub_test_case 'without model' do + test 'form_with' do + visit '/search' + assert page.has_css? '#form_with input#query' + end + end + end +end