Anonymous avatar Anonymous committed 61d2880

[svn] * Updated radio_button so that id's are unique. Brings up to date with Rails
changeset #4925, also fixes #103.

Comments (0)

Files changed (3)

 WebHelpers ChangeLog
 
 0.2.1 (**svn**)
+* Updated radio_button so that id's are unique. Brings up to date with Rails
+  changeset #4925, also fixes #103.
 * More precise distance_of_time_in_words (Follows bottom half of #4989 Rails
   changeset)
 * button_to accepts method keyword so you can PUT and DELETE with it. 

tests/test_form_tag.py

     def test_radio_button(self):
         self.assertEqual(
             radio_button("people", "justin"),
-            '<input id="people" name="people" type="radio" value="justin" />'
+            '<input id="people_justin" name="people" type="radio" value="justin" />'
+        )
+        
+        self.assertEqual(
+            radio_button("num_people", 5),
+            '<input id="num_people_5" name="num_people" type="radio" value="5" />'
         )
 
+        self.assertEqual(
+            radio_button("num_people", 5),
+            '<input id="num_people_5" name="num_people" type="radio" value="5" />'
+        )
+        
+        self.assertEqual(
+            radio_button("gender", "m") + radio_button("gender", "f"),
+            '<input id="gender_m" name="gender" type="radio" value="m" /><input id="gender_f" name="gender" type="radio" value="f" />'
+        )
+        
+        self.assertEqual(
+            radio_button("opinion", "-1") + radio_button("opinion", "1"),
+            '<input id="opinion_-1" name="opinion" type="radio" value="-1" /><input id="opinion_1" name="opinion" type="radio" value="1" />'
+        )
+    
     def test_select(self):
         self.assertEqual(
             select("people", "<option>justin</option>"),

webhelpers/rails/form_tag.py

 """
 Form Tag Helpers
 """
-# Last synced with Rails copy at Revision 4374 on Aug 19th, 2006.
+# Last synced with Rails copy at Revision 4925 on Sept. 6th, 2006.
 
+import re
 from urls import confirm_javascript_function
 from tags import *
 from webhelpers.util import html_escape
     return tag("input", **o)
 
 def radio_button(name, value, checked=False, **options):
-    """Creates a radio button."""
-    o = {'type': 'radio', 'name_': name, 'id': name, 'value': value}
-    o.update(options)
+    """Creates a radio button.
+    
+    The id of the radio button will be set to the name + value with a _ in
+    between to ensure its uniqueness.
+    """
+    pretty_tag_value = re.sub(r'\s', "_", str(value))
+    pretty_tag_value = re.sub(r'(?!-)\W', "", pretty_tag_value).lower()
+    html_options = {'type': 'radio', 'name_': name, 'id': '%s_%s' % (name, pretty_tag_value), 'value': value}
+    html_options.update(options)
     if checked:
-        o["checked"] = "checked"
-    return tag("input", **o)
+        html_options["checked"] = "checked"
+    return tag("input", **html_options)
 
 def submit(value="Save changes", name='commit', confirm=None, disable_with=None, **options):
     """Creates a submit button with the text ``value`` as the caption.
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.