Re: formencode and toscawidgetsforms interaction

"David Smith" <[email protected]>
Newsgroups gmane.comp.python.formencode
Organization TriAx Corp
Message-ID <[email protected]>
Hi all,

I was experimenting with using ToscaWidgetsForms and tried
using a UnicodeString field in a ListForm which proceeded to
die with a Unicode decode error. I traced this to formencode's
UnicodeString validator automatically encoding the unicode
object into utf-8 which genshi then tried to use in a
u''.join([value]) form, which causes the error.

Thus, you cannot currently use non-ascii strings with
ToscaWidgetsForms fields.

I've attached a patch to toscawidgetform's test_forms.py to
demonstrate.

I believe the solution is that UnicodeString's from_python
should not encode the string. Would that cause a problem for
anyone?
--
  David D. Smith

-------------------------------------------------------------------------
Take Surveys. Earn Cash. Influence the Future of IT
Join SourceForge.net's Techsay panel and you'll get the chance to share your
opinions on IT & business topics through brief surveys - and earn cash
http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV

_______________________________________________
FormEncode-discuss mailing list
FormEncode-discuss-5NWGOfrQmneRv+LV9MX5uipxlwaOVQ5f@public.gmane.org
https://lists.sourceforge.net/lists/listinfo/formencode-discuss
(unnamed) (text/x-patch, 2.8 KB)
--- old-toscawidgetsforms/tests/test_forms.py	2007-01-09 16:03:42.000000000 +0900
+++ new-toscawidgetsforms/tests/test_forms.py	2007-01-09 16:03:42.000000000 +0900
@@ -1,9 +1,10 @@
+# -*- coding: utf-8 -*-
 from unittest import TestCase
 from toscawidgets.api import WidgetsList
 from toscawidgets.widgets.forms import *
 from toscawidgets.testutil import WidgetTestCase
 
-from formencode.validators import Int, String
+from formencode.validators import Int, String, UnicodeString
 from formencode import Invalid
 
 
@@ -82,6 +83,8 @@
             engine_name = "genshi"
         children = [
             TextField("name", validator=String(not_empty=True)),
+            TextField("friend", default="Japanese Friend's Name",
+                      validator=UnicodeString()),
             TextField("age", validator=Int()),
             ]
         template = """<form
@@ -95,8 +98,8 @@
         engine_name = "genshi"
 
     class Person(object):
-        def __init__(self, name, age):
-            self.name = name; self.age = age
+        def __init__(self, name, age, friend=None):
+            self.name = name; self.age = age; self.friend = friend
     
 
 
@@ -108,14 +111,35 @@
         value = self.Person('alberto', 'foo')
         self.assertRaises(Invalid, self.widget.render, value)
 
+    def test_can_render_unicode(self):
+        value = self.Person('alberto', 25, u"宮本")
+        self.widget.render(value)
+
+    def test_can_validate_unicode(self):
+        value = {'name': 'alberto', 'friend': u"宮本",
+                 'age': 25}
+        expected = {'name': 'alberto', 'friend': u"宮本",
+                    'age': 25}
+        valid = self.widget.validate(value)
+        self.failUnlessEqual(valid, expected)
+
+    def test_can_render_validated_unicode(self):
+        valid = self.widget.validate({'name': 'alberto',
+                                      'friend': u"宮本", 'age': 25})
+        self.widget.render(valid)
+
+    def test_bad_value_raises_Invalid_when_rendering(self):
+        value = self.Person('alberto', 'foo')
+        self.assertRaises(Invalid, self.widget.render, value)
+
     def test_can_validate(self):
-        value = {'name':'alberto', 'age':25}
-        expected = {'name':'alberto', 'age':25}
+        value = {'name':'alberto', 'age':25, 'friend': u''}
+        expected = {'name':'alberto', 'age':25, 'friend': u''}
         self.failUnlessEqual(self.widget.validate(value), expected)
 
     def test_can_coerce_good_value(self):
-        value = {'name':'alberto', 'age':'25'}
-        expected = {'name':'alberto', 'age':25}
+        value = {'name':'alberto', 'age':'25', 'friend': None}
+        expected = {'name':'alberto', 'age':25, 'friend': u''}
         self.failUnlessEqual(self.widget.validate(value), expected)
 
     def test_bad_value_raises_Invalid(self):
signature.asc (application/pgp-signature, 188 B)
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.6 (GNU/Linux)

iD8DBQFFo00zEJGOueZRHH4RAq0DAKCDfGrMtkqdoEredBbnW7R/ZAs1ZwCeIuim
S8xtVceKdjf5JN6qnAyFL88=
=aJ4r
-----END PGP SIGNATURE-----
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.