diff --git a/lib/simple_form/inputs/collection_input.rb b/lib/simple_form/inputs/collection_input.rb index cc6fe1ea..d1ec9ec6 100644 --- a/lib/simple_form/inputs/collection_input.rb +++ b/lib/simple_form/inputs/collection_input.rb @@ -33,15 +33,19 @@ module SimpleForm end end - # Select components does not allow the required html tag. + # Checkbox components does not use the required html tag. + # See more info here - https://github.com/plataformatec/simple_form/issues/340#issuecomment-2871956 def has_required? - super && input_type != :select + super && (input_options[:include_blank] || multiple?) && input_type != :check_boxes end # Check if :include_blank must be included by default. def skip_include_blank? - (options.keys & [:prompt, :include_blank, :default, :selected]).any? || - options[:input_html].try(:[], :multiple) + (options.keys & [:prompt, :include_blank, :default, :selected]).any? || multiple? + end + + def multiple? + !!options[:input_html].try(:[], :multiple) end # Detect the right method to find the label and value for a collection. diff --git a/test/inputs/collection_input_test.rb b/test/inputs/collection_input_test.rb index bf81f79b..285a49d5 100644 --- a/test/inputs/collection_input_test.rb +++ b/test/inputs/collection_input_test.rb @@ -259,12 +259,36 @@ class CollectionInputTest < ActionView::TestCase end end - test 'collection input with select type should not generate invalid required html attribute' do - with_input_for @user, :name, :select, :collection => ['Jose' , 'Carlos'] + test 'collection input with select type should generate required html attribute only with blank option' do + with_input_for @user, :name, :select, :include_blank => true, :collection => ['Jose' , 'Carlos'] + assert_select 'select.required' + assert_select 'select[required]' + end + + test 'collection input with select type should not generate required html attribute without blank option' do + with_input_for @user, :name, :select, :include_blank => false, :collection => ['Jose' , 'Carlos'] assert_select 'select.required' assert_no_select 'select[required]' end + test 'collection input with select type with multiple attribute should generate required html attribute without blank option' do + with_input_for @user, :name, :select, :include_blank => true, :input_html => {:multiple => true}, :collection => ['Jose' , 'Carlos'] + assert_select 'select.required' + assert_select 'select[required]' + end + + test 'collection input with select type with multiple attribute should generate required html attribute with blank option' do + with_input_for @user, :name, :select, :include_blank => true, :input_html => {:multiple => true}, :collection => ['Jose' , 'Carlos'] + assert_select 'select.required' + assert_select 'select[required]' + end + + test 'collection input with check_boxes type should not generate required html attribute' do + with_input_for @user, :name, :check_boxes, :collection => ['Jose' , 'Carlos'] + assert_select 'input.required' + assert_no_select 'input[required]' + end + test 'input should allow disabled options with a lambda for collection select' do with_input_for @user, :name, :select, :collection => ["Carlos", "Antonio"], :disabled => lambda { |x| x == "Carlos" }