Skip to content

Commit

Permalink
Remove aria-required for required fields in HTML5 (#1823)
Browse files Browse the repository at this point in the history
The reason is that the fields are already assigned the required attribute which would be used by assistive technology to convey the field requirement
  • Loading branch information
aduth authored Jun 21, 2024
1 parent d3a5949 commit c439da9
Show file tree
Hide file tree
Showing 10 changed files with 2 additions and 69 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
* Revert "Speed up input mapping lookup by avoiding rescuing exceptions" from v5.3.0, it caused a regression on dev/test environments with custom inputs.
* Try a slightly different approach to input lookups, without relying on regexp, to see if that helps with performance as originally intended.
* Add support to Ruby 3.3. (no changes required.)
* Remove redundant `aria-required` attribute for required fields. [@aduth](https://github.com/aduth)

## 5.3.0

Expand Down
7 changes: 0 additions & 7 deletions lib/simple_form/components/html5.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,7 @@ def html5(wrapper_options = nil)
@html5 = true

input_html_options[:required] = input_html_required_option
input_html_options[:'aria-required'] = input_html_aria_required_option

input_html_options[:'aria-invalid'] = has_errors? || nil

nil
end

Expand All @@ -25,10 +22,6 @@ def input_html_required_option
!options[:required].nil? ? required_field? : has_required?
end

def input_html_aria_required_option
!options[:required].nil? ? (required_field? || nil) : (has_required? || nil)
end

def has_required?
# We need to check browser_validations because
# some browsers are still checking required even
Expand Down
6 changes: 0 additions & 6 deletions test/inputs/collection_check_boxes_input_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,6 @@ class CollectionCheckBoxesInputTest < ActionView::TestCase
assert_no_select 'input[required]'
end

test 'collection input with check_boxes type does not generate aria-required html attribute' do
with_input_for @user, :name, :check_boxes, collection: %w[Jose Carlos]
assert_select 'input.required'
assert_no_select 'input[aria-required]'
end

test 'input does automatic collection translation for check_box types using defaults key' do
store_translations(:en, simple_form: { options: { defaults: {
gender: { male: 'Male', female: 'Female' }
Expand Down
6 changes: 0 additions & 6 deletions test/inputs/collection_radio_buttons_input_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -186,12 +186,6 @@ class CollectionRadioButtonsInputTest < ActionView::TestCase
assert_select 'input[type=radio][required]'
end

test 'collection input with radio type generates aria-required html attribute' do
with_input_for @user, :name, :radio_buttons, collection: %w[Jose Carlos]
assert_select 'input[type=radio].required'
assert_select 'input[type=radio][aria-required=true]'
end

test 'input radio does not wrap the collection by default' do
with_input_for @user, :active, :radio_buttons

Expand Down
26 changes: 0 additions & 26 deletions test/inputs/collection_select_input_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -283,14 +283,12 @@ class CollectionSelectInputTest < ActionView::TestCase
test "collection input generated aria-label should contain 'true'" do
with_input_for @user, :age, :select, collection: 18..30, prompt: "Please select foo"
assert_select 'select.required'
assert_select 'select[aria-required=true]'
end

test 'collection input with select type does not generate required html attribute without blank option' do
with_input_for @user, :name, :select, include_blank: false, collection: %w[Jose Carlos]
assert_select 'select.required'
assert_no_select 'select[required]'
assert_no_select 'select[aria-required=true]'
end

test 'collection input with select type with multiple attribute generates required html attribute without blank option' do
Expand All @@ -305,30 +303,6 @@ class CollectionSelectInputTest < ActionView::TestCase
assert_select 'select[required]'
end

test 'with a blank option, a collection input of type select has an aria-required html attribute' do
with_input_for @user, :name, :select, include_blank: true, collection: %w[Jose Carlos]
assert_select 'select.required'
assert_select 'select[aria-required=true]'
end

test 'without a blank option, a collection input of type select does not have an aria-required html attribute' do
with_input_for @user, :name, :select, include_blank: false, collection: %w[Jose Carlos]
assert_select 'select.required'
assert_no_select 'select[aria-required]'
end

test 'without a blank option and with a multiple option, a collection input of type select has an aria-required html attribute' do
with_input_for @user, :name, :select, include_blank: false, input_html: { multiple: true }, collection: %w[Jose Carlos]
assert_select 'select.required'
assert_select 'select[aria-required=true]'
end

test 'with a blank option and a multiple option, a collection input of type select has an aria-required html attribute' do
with_input_for @user, :name, :select, include_blank: true, input_html: { multiple: true }, collection: %w[Jose Carlos]
assert_select 'select.required'
assert_select 'select[aria-required]'
end

test 'input allows disabled options with a lambda for collection select' do
with_input_for @user, :name, :select, collection: %w[Carlos Antonio],
disabled: ->(x) { x == "Carlos" }
Expand Down
6 changes: 0 additions & 6 deletions test/inputs/country_input_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,4 @@ class CountryInputTest < ActionView::TestCase
assert_select 'select.required'
assert_select 'select[required]'
end

test 'input does generate select element with aria-required html attribute' do
with_input_for @user, :country, :country
assert_select 'select.required'
assert_select 'select[aria-required]'
end
end
5 changes: 0 additions & 5 deletions test/inputs/datetime_input_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,6 @@ class DateTimeInputWithHtml5Test < ActionView::TestCase
assert_select 'input.required'
assert_select 'input[required]'
end

test 'input has an aria-required html attribute' do
with_input_for @user, :delivery_time, :time, required: true, html5: true
assert_select 'input[aria-required=true]'
end
end

# Tests for datetime, date and time inputs when HTML5 compatibility is enabled in the wrapper.
Expand Down
3 changes: 1 addition & 2 deletions test/inputs/hidden_input_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,10 @@ class HiddenInputTest < ActionView::TestCase
assert_no_select 'label'
end

test 'required/aria-required/optional options does not be generated for hidden inputs' do
test 'required/optional options does not be generated for hidden inputs' do
with_input_for @user, :name, :hidden
assert_no_select 'input.required'
assert_no_select 'input[required]'
assert_no_select 'input[aria-required]'
assert_no_select 'input.optional'
assert_select 'input.hidden#user_name'
end
Expand Down
5 changes: 0 additions & 5 deletions test/inputs/required_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ class RequiredTest < ActionView::TestCase
with_input_for @user, :name, :string
assert_select 'input[type=text].required'
assert_no_select 'input[type=text][required]'
assert_no_select 'input[type=text][aria-required]'
end
end

Expand All @@ -44,7 +43,6 @@ class RequiredTest < ActionView::TestCase
with_input_for @user, :name, :string, required: false
assert_no_select 'input[type=text].required'
assert_no_select 'input[type=text][required]'
assert_no_select 'input[type=text][aria-required]'
end
end

Expand All @@ -53,7 +51,6 @@ class RequiredTest < ActionView::TestCase
with_input_for @user, :name, :string, required: true
assert_select 'input[type=text].required'
assert_select 'input[type=text][required]'
assert_select 'input[type=text][aria-required]'
end
end

Expand All @@ -65,7 +62,6 @@ class RequiredTest < ActionView::TestCase
end
assert_select 'input[type=text].required'
assert_no_select 'input[type=text][required]'
assert_no_select 'input[type=text][aria-required]'
end
end
end
Expand Down Expand Up @@ -151,7 +147,6 @@ class RequiredTest < ActionView::TestCase
concat f.input :name, required: false
end
assert_no_select 'input[type=text][required]'
assert_no_select 'input[type=text][aria-required]'
end
end
end
Expand Down
6 changes: 0 additions & 6 deletions test/inputs/time_zone_input_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,4 @@ class TimeZoneInputTest < ActionView::TestCase
assert_select 'select.required'
assert_select 'select[required]'
end

test 'input does generate select element with aria-required html attribute' do
with_input_for @user, :time_zone, :time_zone
assert_select 'select.required'
assert_select 'select[aria-required]'
end
end

0 comments on commit c439da9

Please sign in to comment.