Skip to content
This repository
Browse code

make text_field and hidden_field omit the value attribute if the deve…

…loper explicitly passes in :value => nil [#4839 state:resolved]

Signed-off-by: Michael Koziarski <michael@koziarski.com>

Conflicts:

	actionpack/lib/action_view/helpers/form_helper.rb
  • Loading branch information...
commit 52c922fad1e7260a4af87409d04055af85df25f8 1 parent da93d69
Michael Koziarski authored June 23, 2010
10  actionpack/lib/action_view/helpers/form_helper.rb
@@ -787,8 +787,8 @@ def to_input_field_tag(field_type, options = {})
787 787
         if field_type == "hidden"
788 788
           options.delete("size")
789 789
         end
790  
-        options["type"] = field_type
791  
-        options["value"] ||= value_before_type_cast(object) unless field_type == "file"
  790
+        options["type"]  ||= field_type
  791
+        options["value"] = options.fetch("value"){ value_before_type_cast(object) } unless field_type == "file"
792 792
         options["value"] &&= html_escape(options["value"])
793 793
         add_default_name_and_id(options)
794 794
         tag("input", options)
@@ -920,14 +920,14 @@ def add_default_name_and_id_for_value(tag_value, options)
920 920
         def add_default_name_and_id(options)
921 921
           if options.has_key?("index")
922 922
             options["name"] ||= tag_name_with_index(options["index"])
923  
-            options["id"]   ||= tag_id_with_index(options["index"])
  923
+            options["id"] = options.fetch("id"){ tag_id_with_index(options["index"]) }
924 924
             options.delete("index")
925 925
           elsif defined?(@auto_index)
926 926
             options["name"] ||= tag_name_with_index(@auto_index)
927  
-            options["id"]   ||= tag_id_with_index(@auto_index)
  927
+            options["id"] = options.fetch("id"){ tag_id_with_index(@auto_index) }
928 928
           else
929 929
             options["name"] ||= tag_name + (options.has_key?('multiple') ? '[]' : '')
930  
-            options["id"]   ||= tag_id
  930
+            options["id"] = options.fetch("id"){ tag_id }
931 931
           end
932 932
         end
933 933
 
10  actionpack/test/template/form_helper_test.rb
@@ -249,6 +249,11 @@ def test_text_field_removing_size
249 249
     assert_dom_equal expected, text_field("post", "title", :maxlength => 35, :size => nil)
250 250
   end
251 251
 
  252
+  def test_text_field_with_nil_value
  253
+    expected = '<input id="post_title" name="post[title]" size="30" type="text" />'
  254
+    assert_dom_equal expected, text_field("post", "title", :value => nil)
  255
+  end
  256
+
252 257
   def test_text_field_doesnt_change_param_values
253 258
     object_name = 'post[]'
254 259
     expected = '<input id="post_123_title" name="post[123][title]" size="30" type="text" value="Hello World" />'
@@ -269,6 +274,11 @@ def test_hidden_field_with_escapes
269 274
       hidden_field("post", "title")
270 275
   end
271 276
 
  277
+  def test_hidden_field_with_nil_value
  278
+    expected = '<input id="post_title" name="post[title]" type="hidden" />'
  279
+    assert_dom_equal expected, hidden_field("post", "title", :value => nil)
  280
+  end
  281
+
272 282
   def test_text_field_with_options
273 283
     assert_dom_equal '<input id="post_title" name="post[title]" type="hidden" value="Something Else" />',
274 284
       hidden_field("post", "title", :value => "Something Else")

0 notes on commit 52c922f

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