Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Allow to configure default form attributes #562

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

### New features

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

### Bugfixes

Expand Down
20 changes: 20 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,26 @@ in `form_with`.

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


## Configuration
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd like to give a little more information to the reader around configuration. I suggest something like a first paragraph that says:

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.

The current configuration options are:

  • default_form_attributes bootstrap_form versions 4.4.0 and older added a role="form" attribute to all forms. The W3C validator will raise a warning on forms with a role="form" attribute.

The actual example will depend on the path we choose for maintaining compatibility for version 4, and enabling the new default for version 5 (which will be the Bootstrap v5 version). See my comments on the pull request.


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

The current configuration options are:

| Option | Default value | Description |
|---------------------------|------------------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| `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. |


Example:
```ruby
# config/initializers/bootstrap_form.rb
BootstrapForm.configure do |c|
c.default_form_attributes = {} # to make forms W3C compliant
end
```

## Form Helpers

`bootstrap_form` provides its own version of the following Rails form helpers:
Expand Down
2 changes: 2 additions & 0 deletions bootstrap_form.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ Gem::Specification.new do |s|
"easy to create beautiful-looking forms using Bootstrap 4"
s.license = "MIT"

s.post_install_message = "Default form attribute role=\"form\" will be dropped in 5.0.0"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍


s.files = `git ls-files -z`.split("\x0").reject do |f|
f.match(%r{^(test)/})
end
Expand Down
21 changes: 16 additions & 5 deletions lib/bootstrap_form.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ module BootstrapForm
extend ActiveSupport::Autoload

eager_autoload do
autoload :Configuration
autoload :FormBuilder
autoload :FormGroupBuilder
autoload :FormGroup
Expand All @@ -21,11 +22,21 @@ module BootstrapForm
autoload :Helpers
end

def self.eager_load!
super
BootstrapForm::Components.eager_load!
BootstrapForm::Helpers.eager_load!
BootstrapForm::Inputs.eager_load!
class << self
def eager_load!
super
BootstrapForm::Components.eager_load!
BootstrapForm::Helpers.eager_load!
BootstrapForm::Inputs.eager_load!
end

def config
@config ||= BootstrapForm::Configuration.new
end

def configure
yield config
end
end

mattr_accessor :field_error_proc
Expand Down
23 changes: 23 additions & 0 deletions lib/bootstrap_form/configuration.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# frozen_string_literal: true

module BootstrapForm
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice.

class Configuration
def default_form_attributes=(attributes)
case attributes
when nil
@default_form_attributes = {}
when Hash
@default_form_attributes = attributes
else
raise ArgumentError, "Unsupported default_form_attributes #{attributes.inspect}"
end
end

def default_form_attributes
return @default_form_attributes if defined? @default_form_attributes

# TODO: Return blank hash ({}) in 5.0.0. Role "form" for form tags is redundant and makes W3C to raise a warning.
{ role: "form" }
end
end
end
6 changes: 3 additions & 3 deletions lib/bootstrap_form/form_builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -58,14 +58,14 @@ def initialize(object_name, object, template, options)
options[:inline_errors] != false
end
@acts_like_form_tag = options[:acts_like_form_tag]
add_form_role_and_form_inline options
add_default_form_attributes_and_form_inline options
super
end
# rubocop:enable Metrics/AbcSize

def add_form_role_and_form_inline(options)
def add_default_form_attributes_and_form_inline(options)
options[:html] ||= {}
options[:html][:role] ||= "form"
options[:html].reverse_merge!(BootstrapForm.config.default_form_attributes)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be nice to make this work reasonably if the user sets default_form_attributes = nil. Extra bonus marks if we nicely handle if the user sets default_form_attributes to something other than a hash. Throwing an error on start-up might be better than waiting until the app actually tries to render a form.


return unless options[:layout] == :inline

Expand Down
32 changes: 32 additions & 0 deletions test/bootstrap_configuration_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
require_relative "./test_helper"

class BootstrapConfigurationTest < ActionView::TestCase
test "has default form attributes" do
config = BootstrapForm::Configuration.new

assert_equal({ role: "form" }, config.default_form_attributes)
end

test "allows to set default_form_attributes with custom value" do
config = BootstrapForm::Configuration.new
config.default_form_attributes = { foo: "bar" }

assert_equal({ foo: "bar" }, config.default_form_attributes)
end

test "allows to set default_form_attributes with nil" do
config = BootstrapForm::Configuration.new
config.default_form_attributes = nil

assert_equal({ }, config.default_form_attributes)
end

test "does not allow to set default_form_attributes with unsupported value" do
config = BootstrapForm::Configuration.new

exception = assert_raises ArgumentError do
config.default_form_attributes = [1,2,3]
end
assert_equal('Unsupported default_form_attributes [1, 2, 3]', exception.message)
end
end
20 changes: 20 additions & 0 deletions test/bootstrap_form_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,26 @@ class BootstrapFormTest < ActionView::TestCase
assert_equivalent_xml expected, bootstrap_form_for(@user, html: { role: "not-a-form" }) { |_f| nil }
end

test "allows to set blank default form attributes via configuration" do
BootstrapForm.config.stubs(:default_form_attributes).returns({})
expected = <<-HTML.strip_heredoc
<form accept-charset="UTF-8" action="/users" class="new_user" id="new_user" method="post">
#{'<input name="utf8" type="hidden" value="&#x2713;"/>' unless ::Rails::VERSION::STRING >= '6'}
</form>
HTML
assert_equivalent_xml expected, bootstrap_form_for(@user) { |_f| nil }
end

test "allows to set custom default form attributes via configuration" do
BootstrapForm.config.stubs(:default_form_attributes).returns({ foo: "bar" })
expected = <<-HTML.strip_heredoc
<form accept-charset="UTF-8" action="/users" class="new_user" foo="bar" id="new_user" method="post">
#{'<input name="utf8" type="hidden" value="&#x2713;"/>' unless ::Rails::VERSION::STRING >= '6'}
</form>
HTML
assert_equivalent_xml expected, bootstrap_form_for(@user) { |_f| nil }
end

test "bootstrap_form_tag acts like a form tag" do
expected = <<-HTML.strip_heredoc
<form accept-charset="UTF-8" action="/users" method="post" role="form">
Expand Down