Enforce minlen and maxlen on "string" fields in HTML5 capable browsers.

pull/7/head
Ferry Boender 10 years ago
parent 9241cbe837
commit 200fea8598
  1. 18
      src/formrender.py
  2. 2
      src/webapp.py

@ -30,10 +30,11 @@ class FormRender(object):
field_tpl = {
"string": u'<input {required} type="text" name="{name}" '
u'value="{value}" size="{size}" '
u'class="{classes}" style="{style}" />',
u'class="{classes}" style="{style}" minlength="{minlen}" '
u' maxlength="{maxlen}" />',
"number": u'<input {required} type="number" min="{minval}" '
u'max="{maxval}" name="{name}" value="{value}" '
u'class="{classes}" style="{style}" />',
u'max="{maxval}" step="any" name="{name}" '
u'value="{value}" class="{classes}" style="{style}" />',
"integer": u'<input {required} type="number" min="{minval}" '
u'max="{maxval}" name="{name}" value="{value}" '
u'class="{classes}" style="{style}" />',
@ -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)

@ -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", '')

Loading…
Cancel
Save