Skip to content

Commit 9ae52df

Browse files
authored
Allow to configure default form attributes (#562)
1 parent 68a5e0f commit 9ae52df

File tree

8 files changed

+117
-9
lines changed

8 files changed

+117
-9
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
### New features
88

9-
* Your contribution here!
9+
* [#562](https://github.com/bootstrap-ruby/bootstrap_form/pull/562): Allow to configure default form attributes - [@sharshenov](https://github.com/sharshenov).
1010

1111
### Bugfixes
1212

README.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,26 @@ in `form_with`.
150150

151151
`form_with` has some important differences compared to `form_for` and `form_tag`, and these differences apply to `bootstrap_form_with`. A good summary of the differences can be found at: https://m.patrikonrails.com/rails-5-1s-form-with-vs-old-form-helpers-3a5f72a8c78a, or in the [Rails documentation](api.rubyonrails.org).
152152

153+
154+
## Configuration
155+
156+
`bootstrap_form` can be used out-of-the-box without any configuration. However, `bootstrap_form` does have an optional configuration file at `config/initializers/bootstrap_form.rb` for setting options that affect all generated forms in an application.
157+
158+
The current configuration options are:
159+
160+
| Option | Default value | Description |
161+
|---------------------------|------------------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
162+
| `default_form_attributes` | `{role: "form"}` | version [2.2.0](https://github.com/bootstrap-ruby/bootstrap_form/blob/master/CHANGELOG.md#220-2014-09-16) added a role="form" attribute to all forms. The W3C validator will raise a **warning** on forms with a role="form" attribute. Set this option to `{}` to make forms be W3C compliant. |
163+
164+
165+
Example:
166+
```ruby
167+
# config/initializers/bootstrap_form.rb
168+
BootstrapForm.configure do |c|
169+
c.default_form_attributes = {} # to make forms W3C compliant
170+
end
171+
```
172+
153173
## Form Helpers
154174

155175
`bootstrap_form` provides its own version of the following Rails form helpers:

bootstrap_form.gemspec

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ Gem::Specification.new do |s|
1515
"easy to create beautiful-looking forms using Bootstrap 4"
1616
s.license = "MIT"
1717

18+
s.post_install_message = "Default form attribute role=\"form\" will be dropped in 5.0.0"
19+
1820
s.files = `git ls-files -z`.split("\x0").reject do |f|
1921
f.match(%r{^(test)/})
2022
end

lib/bootstrap_form.rb

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ module BootstrapForm
1313
extend ActiveSupport::Autoload
1414

1515
eager_autoload do
16+
autoload :Configuration
1617
autoload :FormBuilder
1718
autoload :FormGroupBuilder
1819
autoload :FormGroup
@@ -21,11 +22,21 @@ module BootstrapForm
2122
autoload :Helpers
2223
end
2324

24-
def self.eager_load!
25-
super
26-
BootstrapForm::Components.eager_load!
27-
BootstrapForm::Helpers.eager_load!
28-
BootstrapForm::Inputs.eager_load!
25+
class << self
26+
def eager_load!
27+
super
28+
BootstrapForm::Components.eager_load!
29+
BootstrapForm::Helpers.eager_load!
30+
BootstrapForm::Inputs.eager_load!
31+
end
32+
33+
def config
34+
@config ||= BootstrapForm::Configuration.new
35+
end
36+
37+
def configure
38+
yield config
39+
end
2940
end
3041

3142
mattr_accessor :field_error_proc
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# frozen_string_literal: true
2+
3+
module BootstrapForm
4+
class Configuration
5+
def default_form_attributes=(attributes)
6+
case attributes
7+
when nil
8+
@default_form_attributes = {}
9+
when Hash
10+
@default_form_attributes = attributes
11+
else
12+
raise ArgumentError, "Unsupported default_form_attributes #{attributes.inspect}"
13+
end
14+
end
15+
16+
def default_form_attributes
17+
return @default_form_attributes if defined? @default_form_attributes
18+
19+
# TODO: Return blank hash ({}) in 5.0.0. Role "form" for form tags is redundant and makes W3C to raise a warning.
20+
{ role: "form" }
21+
end
22+
end
23+
end

lib/bootstrap_form/form_builder.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,14 +58,14 @@ def initialize(object_name, object, template, options)
5858
options[:inline_errors] != false
5959
end
6060
@acts_like_form_tag = options[:acts_like_form_tag]
61-
add_form_role_and_form_inline options
61+
add_default_form_attributes_and_form_inline options
6262
super
6363
end
6464
# rubocop:enable Metrics/AbcSize
6565

66-
def add_form_role_and_form_inline(options)
66+
def add_default_form_attributes_and_form_inline(options)
6767
options[:html] ||= {}
68-
options[:html][:role] ||= "form"
68+
options[:html].reverse_merge!(BootstrapForm.config.default_form_attributes)
6969

7070
return unless options[:layout] == :inline
7171

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
require_relative "./test_helper"
2+
3+
class BootstrapConfigurationTest < ActionView::TestCase
4+
test "has default form attributes" do
5+
config = BootstrapForm::Configuration.new
6+
7+
assert_equal({ role: "form" }, config.default_form_attributes)
8+
end
9+
10+
test "allows to set default_form_attributes with custom value" do
11+
config = BootstrapForm::Configuration.new
12+
config.default_form_attributes = { foo: "bar" }
13+
14+
assert_equal({ foo: "bar" }, config.default_form_attributes)
15+
end
16+
17+
test "allows to set default_form_attributes with nil" do
18+
config = BootstrapForm::Configuration.new
19+
config.default_form_attributes = nil
20+
21+
assert_equal({ }, config.default_form_attributes)
22+
end
23+
24+
test "does not allow to set default_form_attributes with unsupported value" do
25+
config = BootstrapForm::Configuration.new
26+
27+
exception = assert_raises ArgumentError do
28+
config.default_form_attributes = [1,2,3]
29+
end
30+
assert_equal('Unsupported default_form_attributes [1, 2, 3]', exception.message)
31+
end
32+
end

test/bootstrap_form_test.rb

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -341,6 +341,26 @@ class BootstrapFormTest < ActionView::TestCase
341341
assert_equivalent_xml expected, bootstrap_form_for(@user, html: { role: "not-a-form" }) { |_f| nil }
342342
end
343343

344+
test "allows to set blank default form attributes via configuration" do
345+
BootstrapForm.config.stubs(:default_form_attributes).returns({})
346+
expected = <<-HTML.strip_heredoc
347+
<form accept-charset="UTF-8" action="/users" class="new_user" id="new_user" method="post">
348+
#{'<input name="utf8" type="hidden" value="&#x2713;"/>' unless ::Rails::VERSION::STRING >= '6'}
349+
</form>
350+
HTML
351+
assert_equivalent_xml expected, bootstrap_form_for(@user) { |_f| nil }
352+
end
353+
354+
test "allows to set custom default form attributes via configuration" do
355+
BootstrapForm.config.stubs(:default_form_attributes).returns({ foo: "bar" })
356+
expected = <<-HTML.strip_heredoc
357+
<form accept-charset="UTF-8" action="/users" class="new_user" foo="bar" id="new_user" method="post">
358+
#{'<input name="utf8" type="hidden" value="&#x2713;"/>' unless ::Rails::VERSION::STRING >= '6'}
359+
</form>
360+
HTML
361+
assert_equivalent_xml expected, bootstrap_form_for(@user) { |_f| nil }
362+
end
363+
344364
test "bootstrap_form_tag acts like a form tag" do
345365
expected = <<-HTML.strip_heredoc
346366
<form accept-charset="UTF-8" action="/users" method="post" role="form">

0 commit comments

Comments
 (0)