Re: Example howto use FormEncode with Webware-1.1b1
Christoph Zwerschke <[email protected]> Tue, 11 May 2010 11:44:47 +0200
| Newsgroups | gmane.comp.python.webware |
|---|---|
| Message-ID | <[email protected]> |
Am 11.05.2010 08:53, schrieb Georg Balmer:
> I got the example from a direct download from formencode.org:
> . [2] svn co http://svn.colorstudy.com/FormEncode/trunk FormEncode
> (examples directory: WebwareExamples/index.py)
Thanks, I was not aware of that example. Seems it was actually written
for Paste WebKit (http://pythonpaste.org/webkit/).
As you noticed it uses htmlform which is deprecated; you must use
htmlfill directly. You must also add a name="color" attribute to the
checkboxes to make these work.
It seems your problem is that the formencode errors are now unicode, so
you must encode these. Here is how I got it working with the current
Webware (I simplified it a bit by not using Webware actions):
class form(Page):
schema = FormSchema()
def awake(self, trans):
Page.awake(self, trans)
try:
fields = self.request().fields()
if 'name' in fields:
fields = self.schema.to_python(fields, self)
else:
fields = self.getDefaults()
except formencode.Invalid, e:
errors = dict((k, v.encode('utf-8'))
for k, v in e.unpack_errors().iteritems())
else:
errors = None
self.rendered_form = formencode.htmlfill.render(form_template,
defaults=fields, errors=errors)
def writeContent(self):
self.write(page_style % self.rendered_form)
def getDefaults(self):
return dict(age='enter your age', color=['blue'])
Let me know how it works for you.
Btw, instead of using a schema, you could also embed the validation info
in the form template and use the SchemaBuilder to parse these.
-- Christoph
------------------------------------------------------------------------------