On May 8, 2007, at 8:46 AM, Christoph Haas wrote:
>
> Dear list...
>
> I'm learning how to use Formencode to validate the input of the HTML
> forms I use. Unfortunatly I have some breakage that I don't know
> how to
> fix.
>
> My controller:
>
> ===================================================
> class DhcpZoneForm(formencode.Schema):
> allow_extra_fields = True
> filter_extra_fields = True
> new_network = formencode.validators.URL(not_empty=True)
>
> class DhcpController(BaseController):
> def new(self):
> return render_response('dhcp/new.mako')
>
> @validate(schema=DhcpZoneForm, form=u'new')
> def new_form(self):
> return Response('Your network is: %s' %
> self.form_result.get('new_network'))
> ===================================================
>
> The Mako template is pretty straight forward. Just a HTML form that is
> supposed to call the "new_form" action with text fields where one
> field
> is "new_network".
>
> Mako template:
> ===================================================
> # -*- coding: utf-8 -*-
> <%inherit file="/base.mako"/>
>
> <h1>New DHCP zone</h1>
>
> ${ h.form(h.url_for(action='new_form')) }
> <table>
> <tr>
> <th>Network</th>
> <td>${ h.text_field('new_network') }</td>
> </tr>
> <tr>
> <th>Comment</th>
> <td>${ h.text_field('new_comment') }</td>
> </tr>
> <tr>
> <th>Standard Lease Time</th>
> <td>${ h.text_field('new_standard_lease') }</td>
> </tr>
> </tr>
> <tr>
> <th>Maximum Lease Time</th>
> <td>${ h.text_field('new_maximum_lease') }</td>
> </tr>
> </table>
> ${ h.submit() }
> </form>
> ===================================================
>
> Now the error that I get is:
>
> ===================================================
> File '<string>', line 1 in <lambda>
> File '/var/lib/python-support/python2.4/pylons/decorators/
> __init__.py', line 106 in wrapper
> response.content = [htmlfill.render(form_content, params, errors)]
Christoph and I discussed this on IRC. Turns out this is a bug with
the validate decorator's interaction with FormEncode-0.7.
The form_content (rendered form) is a raw string as returned from
render_response (and contains non ascii characters). FormEncode-0.7's
errors dictionary contains unicode error messages (new in 0.7 because
FormEncode's error messages are localized via ugettext). When
htmlfill combines these two it blows up due to the non-ascii chars.
> File '/usr/lib/python2.4/site-packages/formencode/htmlfill.py',
> line 70 in render
> p.close()
> File '/usr/lib/python2.4/site-packages/formencode/htmlfill.py',
> line 235 in close
> self._text = ''.join([
> UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in
> position 10: ordinal not in range(128) the form was passed in as an
> encoded string, but some data or error messages were unicode
> strings; the form should be passed in as a unicode string
> ===================================================
One workaround for this is to use unicode form params, to enable this
see: http://pylonshq.com/docs/0.9.5/internationalization.html#request-
parameters
Note that enabling unicode form params may require you to change
other parts of your code that aren't setup to handle unicode, but
this is really the right way to do things anyhow. Pylons 1.0 will
default to coercing form params to unicode.
This works around the issue because @validate (as of Pylons 0.9.5)
automatically coerces the rendered form to unicode when it notices
you're using unicode form params (UnicodeMultiDict).
The proper fix is for @validate to do some similar tinkering with the
form_content/params/errors passed to htmlfill when unicode form
params aren't being used. I'm working on this fix now -- that
workaround isn't feasible for everybody.
Thanks Christoph for finding and helping debug this
>
> In the debug window I clicked on the '>>' to see which code broke:
>
> ===================================================
> try:
> self._text = ''.join([
> t for t in self._content if not isinstance(t, tuple)])
> except UnicodeDecodeError, e:>> self._text = ''.join([
> ===================================================
>
> So I thought it's the self._content that is not correctly encoded in
> unicode and had it print in the debug window. Among many other lines I
> also found parts like these:
>
> ===================================================
> '<li class="navtitel">',
> 'Host-Eintr\xc3\xa4ge',
> '</li>',
> ===================================================
>
> This is a german text that means 'Host-Einträge'. Shouldn't that
> string
> rather be u'Host-Einträge'? What have I missed? I told Mako to use
> UTF8:
>
> config/environment.py:
> ===================================================
> # The following template options are passed to your template engines
> tmpl_options = {}
> tmpl_options['mako.input_encoding'] = 'UTF-8'
> tmpl_options['mako.output_encoding'] = 'UTF-8'
> tmpl_options['mako.default_filters'] = ['decode.utf8']
> ===================================================
>
> Help! :)
--
Philip Jenvey
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"pylons-discuss" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/pylons-discuss?hl=en
-~----------~----~----~----~------~----~------~--~---