Make the HTML generated for collection fields valid #347

Closed
wants to merge 1 commit into
from
Jump to file or symbol
Failed to load files and symbols.
+32 −1
Split
@@ -22,7 +22,11 @@ def translate_required_mark
end
def label
- @builder.label(label_target, label_text, label_html_options)
+ res = @builder.label(label_target, label_text, label_html_options)
+ if !label_html_options.key?(:for) && input_type =~ /^radio|check_boxes$/
+ res = res.sub(/\sfor="[^"]*"/, '').html_safe
+ end
+ res
end
def label_text
@@ -247,6 +247,33 @@ def @controller.action_name; nil; end
assert_select 'label[for=project_name]', /Name/
end
+ test 'label should include for attribute for select collection' do
+ with_label_for @user, :sex, :select, :collection => [:male, :female]
+ assert_select 'label[for=user_sex]'
+ end
+
+ test 'label should not include for attribute for radio collection' do
+ with_label_for @user, :sex, :radio, :collection => [:male, :female]
+ assert_select 'label'
+ assert_no_select 'label[for=user_sex]'
+ end
+
+ test 'label should include for attribute for radio collection when overwritten' do
+ with_label_for @user, :sex, :radio, :collection => [:male, :female], :label_html => { :for => 'sex' }
+ assert_select 'label[for=sex]'
+ end
+
+ test 'label should not include for attribute for check box collection' do
+ with_label_for @user, :hobbies, :check_boxes, :collection => [:sports, :movies, :shopping]
+ assert_select 'label'
+ assert_no_select 'label[for=user_hobbies]'
+ end
+
+ test 'label should include for attribute for check box collection when overwritten' do
+ with_label_for @user, :hobbies, :check_boxes, :collection => [:sports, :movies, :shopping], :label_html => { :for => 'hobbies' }
+ assert_select 'label[for=hobbies]'
+ end
+
test 'label should use i18n properly when object is not present' do
store_translations(:en, :simple_form => { :labels => {
:project => { :name => 'Nome' }