icook / yota

Flexible Python web forms with asynchronous validation
Other
33 stars 3 forks source link

UnicodeEncodeError if non ascii value is submited #72

Closed xen closed 11 years ago

xen commented 11 years ago

If you fill form with non ascii symbols (for example russian) it fail with this traceback:

Traceback (most recent call last):
  File "/Users/xen/Dev/water/cabinet/venv/lib/python2.7/site-packages/flask/app.py", line 1836, in __call__
    return self.wsgi_app(environ, start_response)
  File "/Users/xen/Dev/water/cabinet/venv/lib/python2.7/site-packages/werkzeug/wsgi.py", line 579, in __call__
    return self.app(environ, start_response)
  File "/Users/xen/Dev/water/cabinet/venv/lib/python2.7/site-packages/flask/app.py", line 1820, in wsgi_app
    response = self.make_response(self.handle_exception(e))
  File "/Users/xen/Dev/water/cabinet/venv/lib/python2.7/site-packages/flask/app.py", line 1403, in handle_exception
    reraise(exc_type, exc_value, tb)
  File "/Users/xen/Dev/water/cabinet/venv/lib/python2.7/site-packages/flask/app.py", line 1817, in wsgi_app
    response = self.full_dispatch_request()
  File "/Users/xen/Dev/water/cabinet/venv/lib/python2.7/site-packages/flask/app.py", line 1477, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "/Users/xen/Dev/water/cabinet/venv/lib/python2.7/site-packages/flask/app.py", line 1381, in handle_user_exception
    reraise(exc_type, exc_value, tb)
  File "/Users/xen/Dev/water/cabinet/venv/lib/python2.7/site-packages/flask/app.py", line 1475, in full_dispatch_request
    rv = self.dispatch_request()
  File "/Users/xen/Dev/water/cabinet/venv/lib/python2.7/site-packages/flask/app.py", line 1461, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "/Users/xen/Dev/water/cabinet/water/registry/views.py", line 96, in contract
    success, out = form.validate_render(request.form)
  File "/Users/xen/Dev/water/cabinet/venv/src/yota/src/yota/__init__.py", line 513, in validate_render
    block, invalid = self._gen_validate(data)
  File "/Users/xen/Dev/water/cabinet/venv/src/yota/src/yota/__init__.py", line 388, in _gen_validate
    check.validate()
  File "/Users/xen/Dev/water/cabinet/venv/src/yota/src/yota/validators.py", line 411, in validate
    return self.validator(*self.args, **self.kwargs)
  File "/Users/xen/Dev/water/cabinet/venv/src/yota/src/yota/validators.py", line 47, in __call__
    if len(str(target.data)) > self.max_length:
UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-2: ordinal not in range(128)

I've fixed code that tries to convert unicode strings using str function. Not fully sure about py3 version, since I don't yet switched to in on this project. But for me it at least fixed problem with other languages.

xen commented 11 years ago

Just to clarify, I used here traceback from working project, but if you try with your own example you will get 500, copy-paste string "Привет" to "First Name" field http://64.49.234.90/yota_example/basic and submit form.

icook commented 11 years ago

Thanks for bringing this to my attention. I fixed it a little differently just because that str was kind of a hack in the first place. Things like the Integer validator were doing conversions which then broke other validators that were expecting strings. I'll try and add real conversion like FormEncode has in the next few releases.