Skip to content

Commit

Permalink
Remove role="form" (#571)
Browse files Browse the repository at this point in the history
  • Loading branch information
lcreid authored Jun 27, 2020
1 parent 9ade720 commit 2828cd9
Show file tree
Hide file tree
Showing 11 changed files with 72 additions and 59 deletions.
14 changes: 14 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,20 @@

* Your contribution here!

## [5.0.0.alpha1][]

### Breaking changes

* [#569] Remove `role="form"` from the default generated form HTML so forms pass W3C validation. (Only a breaking change if you depended on the `form` attribute. `bootstrap_form` doesn't depend on it.)

### New features

* Your contribution here!

### Bugfixes

* Your contribution here!

## [4.5.0][] (2020-04-29)

### New features
Expand Down
8 changes: 3 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ To get started, just use the `bootstrap_form_with` helper in place of `form_with
This generates:

```html
<form role="form" action="/users" accept-charset="UTF-8" method="post">
<form action="/users" accept-charset="UTF-8" method="post">
<input name="utf8" type="hidden" value="&#x2713;" />
<div class="mb-3">
<label class="form-label required" for="user_email">Email</label>
Expand All @@ -144,7 +144,6 @@ 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

`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.
Expand All @@ -153,15 +152,14 @@ 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. |

| `default_form_attributes` | | `bootstrap_form` versions 3 and 4 added a role="form" attribute to all forms. The W3C validator will raise a **warning** on forms with a role="form" attribute. `bootstrap_form` version 5 drops this attribute by default. Set this option to `{ role: "form" }` to make forms non-compliant with W3C, but generate the `role="form"` attribute like `bootstrap_form` versions 3 and 4. |

Example:

```ruby
# config/initializers/bootstrap_form.rb
BootstrapForm.configure do |c|
c.default_form_attributes = {} # to make forms W3C compliant
c.default_form_attributes = { role: "form" } # to make forms non-compliant with W3C.
end
```

Expand Down
2 changes: 2 additions & 0 deletions UPGRADE-5.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ We made every effort to make the upgrade from `bootstrap_form` v4 (Bootstrap 4)

Upgrading `bootstrap_form` to version 5 means you must upgrade your whole application to Bootstrap 5. Read the [Bootstrap 5 migration guide](https://v5.getbootstrap.com/docs/5.0/migration/) to see what changes you have to make to your views. This will also help you understand changes you might have to make to your `bootstrap_form` code.

## `bootstrap_form` Version 5 Changes

## No `role="form"` Attribute

As explained in #560, the `role="form"` attribute generated by `bootstrap_4` caused the W3C validator to output a warning. The `role="form"` attribute was deprecated in the 4.5.0 and is being remove completely in 5.0.0. This has no impact on `bootstrap_form` code itself, but may affect your application if it depended on a form having this attribute set. (Issue #569)
3 changes: 1 addition & 2 deletions lib/bootstrap_form/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@ def default_form_attributes=(attributes)
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
8 changes: 4 additions & 4 deletions test/bootstrap_checkbox_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ class BootstrapCheckboxTest < ActionView::TestCase

test "inline checkboxes from form layout" do
expected = <<-HTML.strip_heredoc
<form accept-charset="UTF-8" action="/users" class="new_user col-auto g-3" id="new_user" method="post" role="form">
<form accept-charset="UTF-8" action="/users" class="new_user col-auto g-3" id="new_user" method="post">
#{'<input name="utf8" type="hidden" value="&#x2713;"/>' unless ::Rails::VERSION::STRING >= '6'}
<div class="form-check form-check-inline">
<input name="user[terms]" type="hidden" value="0" />
Expand Down Expand Up @@ -503,7 +503,7 @@ class BootstrapCheckboxTest < ActionView::TestCase
@user.errors.add(:misc, "a box must be checked")

expected = <<-HTML.strip_heredoc
<form accept-charset="UTF-8" action="/users" class="new_user" id="new_user" method="post" role="form">
<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'}
<input id="user_misc" multiple="multiple" name="user[misc][]" type="hidden" value="" />
<div class="mb-3">
Expand Down Expand Up @@ -532,7 +532,7 @@ class BootstrapCheckboxTest < ActionView::TestCase
@user.errors.add(:misc, "error for test")
collection = [Address.new(id: 1, street: "Foo"), Address.new(id: 2, street: "Bar")]
expected = <<-HTML.strip_heredoc
<form accept-charset="UTF-8" action="/users" class="new_user" id="new_user" method="post" role="form">
<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'}
<input id="user_misc" multiple="multiple" name="user[misc][]" type="hidden" value="" />
<div class="mb-3">
Expand All @@ -559,7 +559,7 @@ class BootstrapCheckboxTest < ActionView::TestCase
test "check_box renders error when asked" do
@user.errors.add(:terms, "You must accept the terms.")
expected = <<-HTML.strip_heredoc
<form accept-charset="UTF-8" action="/users" class="new_user" id="new_user" method="post" role="form">
<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'}
<div class="form-check">
<input name="user[terms]" type="hidden" value="0" />
Expand Down
2 changes: 1 addition & 1 deletion test/bootstrap_configuration_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ class BootstrapConfigurationTest < ActionView::TestCase
test "has default form attributes" do
config = BootstrapForm::Configuration.new

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

test "allows to set default_form_attributes with custom value" do
Expand Down
10 changes: 5 additions & 5 deletions test/bootstrap_fields_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ class BootstrapFieldsTest < ActionView::TestCase
test "file fields are wrapped correctly with error" do
@user.errors.add(:misc, "error for test")
expected = <<-HTML.strip_heredoc
<form accept-charset="UTF-8" action="/users" class="new_user" enctype="multipart/form-data" id="new_user" method="post" role="form">
<form accept-charset="UTF-8" action="/users" class="new_user" enctype="multipart/form-data" id="new_user" method="post">
#{'<input name="utf8" type="hidden" value="&#x2713;"/>' unless ::Rails::VERSION::STRING >= '6'}
<div class="mb-3">
<label class="form-label" for="user_misc">Misc</label>
Expand Down Expand Up @@ -310,7 +310,7 @@ class BootstrapFieldsTest < ActionView::TestCase
end

expected = <<-HTML.strip_heredoc
<form accept-charset="UTF-8" action="/users" class="new_user" id="new_user" method="post" role="form">
<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'}
<div class="mb-3">
<label class="form-label" for="user_address_attributes_street">Street</label>
Expand All @@ -331,7 +331,7 @@ class BootstrapFieldsTest < ActionView::TestCase
end

expected = <<-HTML.strip_heredoc
<form accept-charset="UTF-8" action="/users" class="new_user" id="new_user" method="post" role="form">
<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'}
<div class="mb-3">
<label class="form-label" for="user_preferences_favorite_color">Favorite color</label>
Expand All @@ -352,7 +352,7 @@ class BootstrapFieldsTest < ActionView::TestCase
end

expected = <<-HTML.strip_heredoc
<form accept-charset="UTF-8" action="/users" class="new_user" id="new_user" method="post" role="form">
<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'}
<div class="mb-3 row">
<label class="form-label col-form-label col-sm-2" for="user_address_attributes_street">Street</label>
Expand All @@ -376,7 +376,7 @@ class BootstrapFieldsTest < ActionView::TestCase
end

expected = <<-HTML.strip_heredoc
<form accept-charset="UTF-8" action="/users" class="new_user col-auto g-3" id="new_user" method="post" role="form">
<form accept-charset="UTF-8" action="/users" class="new_user col-auto g-3" id="new_user" method="post">
#{'<input name="utf8" type="hidden" value="&#x2713;"/>' unless ::Rails::VERSION::STRING >= '6'}
<div class="mb-3">
<label class="form-label mr-sm-2" for="user_address_attributes_street">Street</label>
Expand Down
8 changes: 4 additions & 4 deletions test/bootstrap_form_group_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ class BootstrapFormGroupTest < ActionView::TestCase
assert @user.invalid?

expected = <<-HTML.strip_heredoc
<form accept-charset="UTF-8" action="/users" class="new_user" id="new_user" method="post" role="form">
<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'}
<div class="mb-3">
<label class="form-label required" for="user_email">Email</label>
Expand Down Expand Up @@ -420,7 +420,7 @@ class BootstrapFormGroupTest < ActionView::TestCase
end

expected = <<-HTML.strip_heredoc
<form accept-charset="UTF-8" action="/users" class="new_user" id="new_user" method="post" role="form">
<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'}
<div class="mb-3">
<div class="form-check">
Expand Down Expand Up @@ -479,7 +479,7 @@ class BootstrapFormGroupTest < ActionView::TestCase
end

expected = <<-HTML.strip_heredoc
<form accept-charset="UTF-8" action="/users" class="new_user" id="new_user" method="post" role="form">
<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'}
<div class="mb-3 none-margin">
<label class="form-label required" for="user_email">Email</label>
Expand Down Expand Up @@ -583,7 +583,7 @@ class BootstrapFormGroupTest < ActionView::TestCase

test "custom form group layout option" do
expected = <<-HTML.strip_heredoc
<form accept-charset="UTF-8" action="/users" class="new_user" id="new_user" method="post" role="form">
<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'}
<div class="mb-3 col-auto g-3">
<label class="form-label mr-sm-2 required" for="user_email">Email</label>
Expand Down
Loading

0 comments on commit 2828cd9

Please sign in to comment.