select can have required option, closes #340

This commit is contained in:
Vasiliy Ermolovich 2011-12-01 21:40:01 +03:00
parent 0dc1dff256
commit 2069b30b10
2 changed files with 34 additions and 6 deletions

View File

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

View File

@ -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" }