From 200fea859836ba26c468f7161fd7fbda17a51fc4 Mon Sep 17 00:00:00 2001 From: Ferry Boender Date: Tue, 23 Jun 2015 23:03:31 +0200 Subject: [PATCH] Enforce minlen and maxlen on "string" fields in HTML5 capable browsers. --- src/formrender.py | 18 ++++++++++-------- src/webapp.py | 2 ++ 2 files changed, 12 insertions(+), 8 deletions(-) diff --git a/src/formrender.py b/src/formrender.py index 8b34fb4..b63e435 100644 --- a/src/formrender.py +++ b/src/formrender.py @@ -30,10 +30,11 @@ class FormRender(object): field_tpl = { "string": u'', + u'class="{classes}" style="{style}" minlength="{minlen}" ' + u' maxlength="{maxlen}" />', "number": u'', + u'max="{maxval}" step="any" name="{name}" ' + u'value="{value}" class="{classes}" style="{style}" />', "integer": u'', @@ -97,16 +98,17 @@ class FormRender(object): method = getattr(self, method_name, None) return method(**params) - def r_field_string(self, name, value, size=50, required=False, - classes=None, style=""): + def r_field_string(self, name, value, minlen=None, maxlen=None, size=50, + required=False, classes=None, style=""): """ Render a string field to HTML. """ if classes is None: classes = [] tpl = self.field_tpl['string'] - return tpl.format(name=name, value=value, size=size, required=required, - classes=classes, style=style) + return tpl.format(name=name, value=value, minlen=minlen, maxlen=maxlen, + size=size, required=required, classes=classes, + style=style) def r_field_number(self, name, value, minval=None, maxval=None, required=False, classes=None, style=""): @@ -137,7 +139,7 @@ class FormRender(object): """ if classes is None: classes = [] - tpl = self.field_tpl['integer'] + tpl = self.field_tpl['float'] return tpl.format(name=name, value=value, minval=minval, maxval=maxval, required=required, classes=classes, style=style) diff --git a/src/webapp.py b/src/webapp.py index eb850b9..7de8c52 100644 --- a/src/webapp.py +++ b/src/webapp.py @@ -379,6 +379,8 @@ class ScriptFormWebApp(WebAppHandler): if field['type'] == 'string': params['size'] = field.get('size', '') + params['minlen'] = field.get('minlen', '') + params['maxlen'] = field.get('maxlen', '') if field['type'] in ('number', 'integer', 'float', 'password'): params['minval'] = field.get("min", '')