From d4308b9d09e8faa3f6cbb2bbc04e32000f05a73c Mon Sep 17 00:00:00 2001 From: Ferry Boender Date: Fri, 1 May 2015 17:23:34 +0200 Subject: [PATCH] Allow prefilling of form values through url params. --- src/scriptform.py | 66 +++++++++++++++++++++++++++++------------------ 1 file changed, 41 insertions(+), 25 deletions(-) diff --git a/src/scriptform.py b/src/scriptform.py index ee2416a..9ea57e0 100755 --- a/src/scriptform.py +++ b/src/scriptform.py @@ -658,69 +658,85 @@ class ScriptFormWebApp(WebAppHandler): self.end_headers() self.wfile.write(output.encode('utf8')) - def h_form(self, form_name, errors={}): + def h_form(self, form_name, errors={}, **form_values): """ Render a form. """ + form_config = self.scriptform.get_form_config() if not self.auth(): return field_tpl = { - "string": u'', - "number": u'', - "integer": u'', - "float": u'', - "date": u'', - "file": u'', - "password": u'', - "text": u'', - "select": u'', - "checkbox": u'', + "string": u'', + "number": u'', + "integer": u'', + "float": u'', + "date": u'', + "file": u'', + "password": u'', + "text": u'', + "select": u'', + "checkbox": u'', "radio": u'{3}
', } def render_field(field, errors): tpl = field_tpl[field['type']] + field_value = form_values.get(field['name'], '') required = u'' if field.get('required', None): required = 'required' if field['type'] == 'string': - input = tpl.format(required, field['name']) + input = tpl.format(required, field['name'], field_value) + print input elif field['type'] == 'number' or \ field['type'] == 'integer' or \ field['type'] == 'float': input = tpl.format(required, field.get('min', ''), field.get('max', ''), - field['name']) + field['name'], + field_value) elif field['type'] == 'date': - input = tpl.format(required, field['name']) + input = tpl.format(required, field['name'], field_value) elif field['type'] == 'file': - input = tpl.format(required, field['name']) + input = tpl.format(required, field['name'], field_value) elif field['type'] == 'password': - input = tpl.format(required, field.get('minlen', ''), field['name']) + input = tpl.format(required, field.get('minlen', ''), field['name'], field_value) elif field['type'] == 'radio': radio_elems = [] checked = u'checked' for option in field['options']: - radio_elems.append(tpl.format(checked, field['name'], option[0], option[1])) + if field['name'] in form_values: + # If a value was passed in, set the radio to checked if + # this is that value. + if form_values[field['name']] == option[0]: + checked = u'checked' + else: + checked = u'' + radio_elems.append(tpl.format(checked, field['name'], option[0], option[1], field_value)) checked = u'' # Check first radio option input = u''.join(radio_elems) elif field['type'] == 'checkbox': - input = tpl.format(required, field['name']) + input = tpl.format(required, field['name'], field_value) elif field['type'] == 'text': rows = field.get('rows', 5) cols = field.get('cols', 80) - input = tpl.format( - required, - field['name'], - rows, - cols - ) + input = tpl.format(required, + field['name'], + rows, + cols, + field_value) elif field['type'] == 'select': - options = u''.join([tpl.format(o[0], o[1]) for o in field['options']]) + options = [] + selected = '' + for option in field['options']: + if field['name'] in form_values and form_values[field['name']] == option[0]: + selected = 'selected' + options.append(tpl.format(option[0], selected, option[1])) + selected = '' input = u''.format(required, field['name'], options) else: raise ValueError("Unsupported field type: {0}".format(