From eeb77a4605ab6c6103215f7a468de24ea4019c04 Mon Sep 17 00:00:00 2001 From: Sean Doyle Date: Tue, 13 Jan 2026 10:05:02 -0500 Subject: [PATCH] Change `form_with` extension's default `:model` option As of `rails@~>7.2.0`, the `form_with` method [rejects][] `:model` options that are `nil`. To achieve compatibility with the new support, this commit modifies the form builder extension to change the default `:model` keyword argument to `model: false` rather than `model: nil`. To exercise that behavior, this commit introduces the `FormsController` to the test suite, along with `#new` and `#create` routes. [rejects]: https://github.com/rails/rails/commit/12b093df63d2319fb9163f3739dbd04693f19613 --- CHANGELOG.md | 4 ++++ lib/html5_validators/action_view/form_helpers.rb | 2 +- test/fake_app.rb | 10 ++++++++++ test/features/active_model_validation_test.rb | 9 +++++++++ 4 files changed, 24 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9166533..86830f8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,7 @@ +## Unreleased + +* Default `form_with` extension's `:model` option to `false` instead of `nil` [@seanpdoyle] + ## 1.9.0 * Added support for `form_with` [@DialBird] diff --git a/lib/html5_validators/action_view/form_helpers.rb b/lib/html5_validators/action_view/form_helpers.rb index 952c048..52c132e 100644 --- a/lib/html5_validators/action_view/form_helpers.rb +++ b/lib/html5_validators/action_view/form_helpers.rb @@ -13,7 +13,7 @@ def form_for(record, options = {}, &block) end if Rails::VERSION::STRING >= '5.1' - def form_with(model: nil, scope: nil, url: nil, format: nil, **options) + 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 diff --git a/test/fake_app.rb b/test/fake_app.rb index d1097e2..ebed084 100644 --- a/test/fake_app.rb +++ b/test/fake_app.rb @@ -32,6 +32,7 @@ class Html5ValidatorsTestApp < Rails::Application get :new_with_required_false end end + resources :forms, only: [:new, :create] end # models @@ -213,6 +214,15 @@ def new_with_required_false ERB end end +class FormsController < ApplicationController + def new + render inline: <<-ERB +<%= form_with url: forms_path, id: 'form_with' do |f| %> +<%= f.button %> +<% end %> + ERB + end +end # helpers module ApplicationHelper; end diff --git a/test/features/active_model_validation_test.rb b/test/features/active_model_validation_test.rb index 2154b2d..8e2d09d 100644 --- a/test/features/active_model_validation_test.rb +++ b/test/features/active_model_validation_test.rb @@ -125,4 +125,13 @@ class ActiveModelValidationTest < ActionDispatch::IntegrationTest end end end + + if Rails::VERSION::STRING >= '5.1' + sub_test_case 'without model' do + test 'new form' do + visit '/forms/new' + assert page.has_css? '#form_with button' + end + end + end end