forms: check max length against characters, not bytes (#22175)

This commit is contained in:
Frédéric Péters 2018-03-03 11:49:52 +01:00
parent 392039216a
commit 85d0aa04fa
2 changed files with 18 additions and 2 deletions

View File

@ -1,3 +1,5 @@
# -*- coding: utf-8 -*-
import json
import pytest
import hashlib
@ -3160,6 +3162,18 @@ def test_form_text_field_submit(pub):
data = formdef.data_class().get(data_id)
assert data.data['0'] is None
# check max length
formdef.fields = [fields.TextField(id='0', label='string', type='text', maxlength=10)]
formdef.store()
resp = get_app(pub).get('/test/')
resp.forms[0]['f0'] = 'x' * 11
resp = resp.forms[0].submit('submit')
assert 'too many characters (limit is 10)' in resp.body
# check it counts characters, not bytes
resp.forms[0]['f0'] = u'' * 10
resp = resp.forms[0].submit('submit')
assert 'Check values then click submit.' in resp.body
def test_form_items_datasource(pub):
formdef = create_formdef()
formdef.fields = [fields.ItemsField(id='1', label='items',

View File

@ -490,8 +490,10 @@ class TextWidget(quixote.form.TextWidget):
maxlength = int(self.attrs.get('maxlength', 0))
except (TypeError, ValueError):
maxlength = 0
if maxlength and len(self.value) > maxlength:
self.error = _('too many characters (limit is %d)') % maxlength
if maxlength:
uvalue = self.value.decode(get_publisher().site_charset)
if len(uvalue) > maxlength:
self.error = _('too many characters (limit is %d)') % maxlength
if self.validation_function:
try:
self.validation_function(self.value)