Permalink
Browse files

Merge pull request #802 from kchien/hidden_input

Hidden input for multiple selects in 1.2-stable
  • Loading branch information...
2 parents 00d4311 + 6b2d38c commit 5459c1994d82037f40155d68624f8328e3d35b38 @justinfrench committed Mar 3, 2012
Showing with 19 additions and 0 deletions.
  1. +10 −0 lib/formtastic.rb
  2. +9 −0 spec/inputs/select_input_spec.rb
View
@@ -843,11 +843,21 @@ def select_input(method, options)
select(input_name, collection, strip_formtastic_options(options), html_options)
end
+ if html_options[:multiple]
+ select_html = create_hidden_field_for_multiple_select(input_name) << select_html
+ end
+
label_options = options_for_label(options).merge(:input_name => input_name)
label_options[:for] ||= html_options[:id]
label(method, label_options) << select_html
end
+ # Outputs a custom hidden field for multiple selects
+ def create_hidden_field_for_multiple_select(method) #:nodoc:
+ input_name = "#{object_name}[#{method.to_s}][]"
+ template.hidden_field_tag(input_name, '')
+ end
+
# Outputs a timezone select input as Rails' time_zone_select helper. You
# can give priority zones as option.
#
@@ -370,6 +370,15 @@
output_buffer.concat(@form) if Formtastic::Util.rails3?
output_buffer.should have_tag('form li select option[@selected]', :count => 1)
end
+
+ it 'should have a hidden input for multiple selects' do
+ @form = semantic_form_for(@fred) do |builder|
+ concat(builder.input(:posts, :as => :select, :multiple => true, :include_blank => true))
+ end
+
+ output_buffer.concat(@form) if Formtastic::Util.rails3?
+ output_buffer.should have_tag("form li input[@type='hidden'][@name='author[post_ids][]']")
+ end
end
describe 'for a has_and_belongs_to_many association' do

0 comments on commit 5459c19

Please sign in to comment.