Author: ianb
Date: 2008-05-05 12:53:17 -0600 (Mon, 05 May 2008)
New Revision: 3414

Modified:
   FormEncode/trunk/docs/news.txt
   FormEncode/trunk/formencode/htmlfill.py
Log:
compare values in checkboxes, etc, properly, even if the defaults aren't given 
as strings

Modified: FormEncode/trunk/docs/news.txt
===================================================================
--- FormEncode/trunk/docs/news.txt      2008-05-04 14:08:34 UTC (rev 3413)
+++ FormEncode/trunk/docs/news.txt      2008-05-05 18:53:17 UTC (rev 3414)
@@ -15,6 +15,9 @@
 
 * Added ``formencode.htmlrename``, which renames HTML inputs.
 
+* In ``formencode.htmlfill``, non-string values are compared usefully
+  (e.g., a select box with integer values).
+
 1.0.1
 -----
 

Modified: FormEncode/trunk/formencode/htmlfill.py
===================================================================
--- FormEncode/trunk/formencode/htmlfill.py     2008-05-04 14:08:34 UTC (rev 
3413)
+++ FormEncode/trunk/formencode/htmlfill.py     2008-05-05 18:53:17 UTC (rev 
3414)
@@ -194,6 +194,24 @@
         self.encoding = encoding
         self.prefix_error = prefix_error
 
+    def str_compare(self, str1, str2):
+        """
+        Compare the two objects as strings (coercing to strings if necessary).
+        Also uses encoding to compare the strings.
+        """
+        if not isinstance(str1, basestring):
+            if hasattr(str1, '__unicode__'):
+                str1 = unicode(str1)
+            else:
+                str1 = str(str1)
+        if type(str1) == type(str2):
+            return str1 == str2
+        if isinstance(str1, unicode):
+            str1 = str1.encode(self.encoding)
+        else:
+            str2 = str2.encode(self.encoding)
+        return str1 == str2
+
     def close(self):
         self.handle_misc(None)
         RewritingParser.close(self)
@@ -346,7 +364,7 @@
             self.skip_next = True
             self.add_key(name)
         elif t == 'radio':
-            if str(value) == self.get_attr(attrs, 'value'):
+            if self.str_compare(value, self.get_attr(attrs, 'value')):
                 self.set_attr(attrs, 'checked', 'checked')
             else:
                 self.del_attr(attrs, 'checked')
@@ -458,9 +476,9 @@
                 return True
         if hasattr(obj, '__iter__'):
             for inner in obj:
-                if str(inner) == value:
+                if self.str_compare(inner, value):
                     return True
-        return str(obj) == value
+        return self.str_compare(obj, value)
 
     def write_marker(self, marker):
         self._content.append((marker,))


-------------------------------------------------------------------------
This SF.net email is sponsored by the 2008 JavaOne(SM) Conference 
Don't miss this year's exciting event. There's still time to save $100. 
Use priority code J8TL2D2. 
http://ad.doubleclick.net/clk;198757673;13503038;p?http://java.sun.com/javaone
_______________________________________________
FormEncode-CVS mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/formencode-cvs

Reply via email to