Permalink
Browse files

select can have required option, closes #340

  • Loading branch information...
1 parent 0dc1dff commit 2069b30b10a2692cd12477a5863e16e13cab4ea8 @nashby committed Dec 1, 2011
Showing with 34 additions and 6 deletions.
  1. +8 −4 lib/simple_form/inputs/collection_input.rb
  2. +26 −2 test/inputs/collection_input_test.rb
@@ -33,15 +33,19 @@ def collection
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.
@@ -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" }

0 comments on commit 2069b30

Please sign in to comment.