diff --git a/src/scriptform.py b/src/scriptform.py index 6920f42..79bd4cb 100755 --- a/src/scriptform.py +++ b/src/scriptform.py @@ -9,6 +9,8 @@ # - Send responses using self.send_ if possible # - Maintain order of forms in form configuration. # - NOt possible right now to auto prefir dates. +# - Visually distinguish required fields. +# - Allow custom CSS import sys import optparse @@ -686,92 +688,82 @@ class ScriptFormWebApp(WebAppHandler): return field_tpl = { - "string": u'', - "number": u'', - "integer": u'', - "float": u'', - "date": u'', - "file": u'', - "password": u'', - "text": u'', - "select": u'', - "checkbox": u'', - "radio": u'{3}
', + "string": u'', + "number": u'', + "integer": u'', + "float": u'', + "date": u'', + "file": u'', + "password": u'', + "text": u'', + "select": u'', + "checkbox": u'', + "radio": u'{title}
', } def render_field(field, errors): tpl = field_tpl[field['type']] - field_value = form_values.get(field['name'], '') - required = u'' + params = { + "required": u"", + "value": form_values.get(field['name'], ''), + "classes": "", + "name": field['name'], + "min": field.get("min", ''), # number, float + "max": field.get("max", ''), # number, float + "rows": field.get('rows', 5), # text + "cols": field.get('cols', 80), # text + } if field.get('required', None): required = 'required' + if field.get('hidden', None): + params['classes'] += 'hidden ' - if field['type'] == 'string': - input = tpl.format(required, field['name'], field_value) - 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_value) - elif field['type'] == 'date': - input = tpl.format(required, field['name'], field_value) - elif field['type'] == 'file': - input = tpl.format(required, field['name'], field_value) - elif field['type'] == 'password': - input = tpl.format(required, field.get('minlen', ''), field['name'], field_value) + if field['type'] in ('string', 'number', 'integer', 'float', 'date', 'file', 'password', 'text'): + input = tpl.format(**params) elif field['type'] == 'radio': + # Check first radio button if no other one is checked + if not form_values.get(field['name'], None): + checked_value = field['options'][0][0] + else: + checked_value = form_values[field['name']] radio_elems = [] - checked = u'checked' for option in field['options']: - 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 + params['value'] = option[0] + params['title'] = option[1] + if checked_value == option[0]: + params['checked'] = u'checked' + else: + params['checked'] = u'' + radio_elems.append(tpl.format(**params)) input = u''.join(radio_elems) elif field['type'] == 'checkbox': - checked = '' + params['checked'] = '' if field['name'] in form_values and form_values[field['name']] == 'on': - checked = 'checked' - input = tpl.format(required, checked, field['name']) - elif field['type'] == 'text': - rows = field.get('rows', 5) - cols = field.get('cols', 80) - input = tpl.format(required, - field['name'], - rows, - cols, - field_value) + params['checked'] = u'checked' + input = tpl.format(**params) elif field['type'] == 'select': options = [] selected = '' for option in field['options']: + params['value'] = option[0] + params['title'] = option[1] + params['selected'] = u'' 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) + params['selected'] = u'selected' + options.append(tpl.format(**params)) + params['options'] = ''.join(options) + input = u''.format(**params) else: raise ValueError("Unsupported field type: {0}".format( field['type']) ) - classes = '' - if 'hidden' in field and field['hidden']: - classes += 'hidden ' - if field['type'] != 'checkbox': html = html_field else: html = html_field_checkbox - return (html.format(classes=classes, + return (html.format(classes=params['classes'], title=field['title'], input=input, errors=u', '.join(errors)))