Skip to content
Browse files

Only add the "required" HTML 5 attribute for valid inputs, disable in…

… selects (not allowed). Closes #141
  • Loading branch information...
1 parent bfd25a9 commit bc2f3e73e8417bc2b6d8fbb6cf86ef57db324ed3 @carlosantoniodasilva carlosantoniodasilva committed Dec 10, 2010
View
7 lib/simple_form/inputs/base.rb
@@ -28,7 +28,7 @@ def initialize(builder, attribute_name, column, input_type, options = {})
@reflection = options.delete(:reflection)
@options = options
@input_html_options = html_options_for(:input, input_html_classes).tap do |o|
- o[:required] = true if attribute_required?
+ o[:required] = true if has_required?
o[:disabled] = true if disabled?
end
end
@@ -71,6 +71,11 @@ def attribute_required?
end
end
+ # Whether this input is valid for HTML 5 required attribute.
+ def has_required?
+ attribute_required?
+ end
+
def has_validators?
attribute_name && object.class.respond_to?(:validators_on)
end
View
5 lib/simple_form/inputs/collection_input.rb
@@ -31,6 +31,11 @@ def collection
@collection ||= (options.delete(:collection) || self.class.boolean_collection).to_a
end
+ # Select components does not allow the required html tag.
+ def has_required?
+ super && input_type != :select
+ end
+
# Check if :include_blank must be included by default.
def skip_include_blank?
(options.keys & [:prompt, :include_blank, :default, :selected]).any? ||
View
4 lib/simple_form/inputs/date_time_input.rb
@@ -7,6 +7,10 @@ def input
private
+ def has_required?
+ false
+ end
+
def label_target
case input_type
when :date, :datetime
View
6 lib/simple_form/inputs/priority_input.rb
@@ -12,9 +12,13 @@ def input_priority
protected
+ def has_required?
+ false
+ end
+
def skip_include_blank?
super || input_priority.present?
end
end
end
-end
+end
View
4 test/form_builder_test.rb
@@ -390,8 +390,8 @@ def with_association_for(object, *args)
end
test 'builder should generate a hint component tag for the given text for a model with ActiveModel::Validations' do
- with_hint_for @validating_user, 'Hello World!'
- assert_select 'span.hint', 'Hello World!'
+ with_hint_for @validating_user, 'Hello World!'
+ assert_select 'span.hint', 'Hello World!'
end
test 'builder should generate a hint component tag for the given text' do
View
24 test/inputs_test.rb
@@ -321,6 +321,12 @@ def with_input_for(object, attribute_name, type, options={})
assert_no_select 'select option[value=]', /^$/
end
+ test 'priority input should not generate invalid required html attribute' do
+ with_input_for @user, :country, :country
+ assert_select 'select.required'
+ assert_no_select 'select[required]'
+ end
+
# DateTime input
test 'input should generate a datetime select by default for datetime attributes' do
with_input_for @user, :created_at, :datetime
@@ -395,6 +401,12 @@ def with_input_for(object, attribute_name, type, options={})
assert_select 'label[for=project_created_at_4i]'
end
+ test 'date time input should not generate invalid required html attribute' do
+ with_input_for @user, :delivery_time, :time, :required => true
+ assert_select 'select.required'
+ assert_no_select 'select[required]'
+ end
+
# CollectionInput
test 'input should generate boolean radio buttons by default for radio types' do
with_input_for @user, :active, :radio
@@ -566,6 +578,18 @@ def with_input_for(object, attribute_name, type, options={})
assert_select 'select option[value=carlos]', 'carlos'
end
+ test 'collection input with radio type should generate required html attribute' do
+ with_input_for @user, :name, :radio, :collection => ['Jose' , 'Carlos']
+ assert_select 'input[type=radio].required'
+ assert_select 'input[type=radio][required]'
+ end
+
+ test 'collection input with select type should not generate invalid required html attribute' do
+ with_input_for @user, :name, :select, :collection => ['Jose' , 'Carlos']
+ assert_select 'select.required'
+ assert_no_select 'select[required]'
+ end
+
# With no object
test 'input should be generated properly when object is not present' do
with_input_for :project, :name, :string

0 comments on commit bc2f3e7

Please sign in to comment.
Something went wrong with that request. Please try again.