Implemented field styles that lets the user specify inline CSS style for a form field.

pull/7/head
Ferry Boender 10 years ago
parent c0634564c4
commit 64e2afeeda
  1. 3
      doc/MANUAL.md
  2. 3
      examples/megacorp_acc/megacorp_acc.json
  3. 73
      src/scriptform.py

@ -154,6 +154,9 @@ Structurally, they are made up of the following elements:
view it, if you know its name. This is useful for other forms to view it, if you know its name. This is useful for other forms to
redirect to this forms and such. redirect to this forms and such.
- **`style`**: A string of inline CSS which will be applied to the field.
**Optional**, **String**.
- **`fields`**: List of fields in the form. Each field is a dictionary. - **`fields`**: List of fields in the form. Each field is a dictionary.
**Required**, **List of dictionaries**. **Required**, **List of dictionaries**.

@ -25,7 +25,8 @@
{ {
"name": "email_address", "name": "email_address",
"title": "What's your email address?", "title": "What's your email address?",
"type": "string" "type": "string",
"style": "width: 300px;"
} }
] ]
}, },

@ -633,18 +633,18 @@ class WebAppHandler(BaseHTTPRequestHandler):
class FormRender(): class FormRender():
field_tpl = { field_tpl = {
"string": u'<input {required} type="text" name="{name}" value="{value}" size="{size}" class="{classes}" />', "string": u'<input {required} type="text" name="{name}" value="{value}" size="{size}" class="{classes}" style="{style}" />',
"number": u'<input {required} type="number" min="{min}" max="{max}" name="{name}" value="{value}" class="{classes}" />', "number": u'<input {required} type="number" min="{min}" max="{max}" name="{name}" value="{value}" class="{classes}" style="{style}" />',
"integer": u'<input {required} type="number" min="{min}" max="{max}" name="{name}" value="{value}" class="{classes}" />', "integer": u'<input {required} type="number" min="{min}" max="{max}" name="{name}" value="{value}" class="{classes}" style="{style}" />',
"float": u'<input {required} type="number" min="{min}" max="{max}" step="any" name="{name}" value="{value}" class="{classes}" />', "float": u'<input {required} type="number" min="{min}" max="{max}" step="any" name="{name}" value="{value}" class="{classes}" style="{style}" />',
"date": u'<input {required} type="date" name="{name}" value="{value}" class="{classes}" />', "date": u'<input {required} type="date" name="{name}" value="{value}" class="{classes}" style="{style}" />',
"file": u'<input {required} type="file" name="{name}" class="{classes}" />', "file": u'<input {required} type="file" name="{name}" class="{classes}" style="{style}" />',
"password": u'<input {required} type="password" min="{min}" name="{name}" value="{value}" class="{classes}" />', "password": u'<input {required} type="password" min="{min}" name="{name}" value="{value}" class="{classes}" style="{style}" />',
"text": u'<textarea {required} name="{name}" rows="{rows}" cols="{cols}" class="{classes}">{value}</textarea>', "text": u'<textarea {required} name="{name}" rows="{rows}" cols="{cols}" style="{style}" class="{classes}">{value}</textarea>',
"radio_option": u'<input {checked} type="radio" name="{name}" value="{value}" class="{classes}">{label}<br/>', "radio_option": u'<input {checked} type="radio" name="{name}" value="{value}" class="{classes} style="{style}"">{label}<br/>',
"select_option": u'<option value="{value}" {selected}>{label}</option>', "select_option": u'<option value="{value}" style="{style}" {selected}>{label}</option>',
"select": u'<select name="{name}" class="{classes}">{select_elems}</select>', "select": u'<select name="{name}" class="{classes}" style="{style}">{select_elems}</select>',
"checkbox": u'<input {checked} type="checkbox" name="{name}" value="on" class="{classes}" />', "checkbox": u'<input {checked} type="checkbox" name="{name}" value="on" class="{classes} style="{style}"" />',
} }
def __init__(self, form_def): def __init__(self, form_def):
@ -676,63 +676,63 @@ class FormRender():
method = getattr(self, method_name, None) method = getattr(self, method_name, None)
return method(**params) return method(**params)
def r_field_string(self, name, value, size=50, required=False, classes=[]): def r_field_string(self, name, value, size=50, required=False, classes=[], style=""):
tpl = self.field_tpl['string'] tpl = self.field_tpl['string']
return tpl.format(name=name, value=value, size=size, required=required, classes=classes) return tpl.format(name=name, value=value, size=size, required=required, classes=classes, style=style)
def r_field_number(self, name, value, min=None, max=None, required=False, classes=[]): def r_field_number(self, name, value, min=None, max=None, required=False, classes=[], style=""):
tpl = self.field_tpl['number'] tpl = self.field_tpl['number']
return tpl.format(name=name, value=value, min=min, max=max, required=required, classes=classes) return tpl.format(name=name, value=value, min=min, max=max, required=required, classes=classes, style=style)
def r_field_integer(self, name, value, min=None, max=None, required=False, classes=[]): def r_field_integer(self, name, value, min=None, max=None, required=False, classes=[], style=""):
tpl = self.field_tpl['integer'] tpl = self.field_tpl['integer']
return tpl.format(name=name, value=value, min=min, max=max, required=required, classes=classes) return tpl.format(name=name, value=value, min=min, max=max, required=required, classes=classes, style=style)
def r_field_float(self, name, value, min=None, max=None, required=False, classes=[]): def r_field_float(self, name, value, min=None, max=None, required=False, classes=[], style=""):
tpl = self.field_tpl['integer'] tpl = self.field_tpl['integer']
return tpl.format(name=name, value=value, min=min, max=max, required=required, classes=classes) return tpl.format(name=name, value=value, min=min, max=max, required=required, classes=classes, style=style)
def r_field_date(self, name, value, required=False, classes=[]): def r_field_date(self, name, value, required=False, classes=[], style=""):
tpl = self.field_tpl['date'] tpl = self.field_tpl['date']
return tpl.format(name=name, value=value, required=required, classes=classes) return tpl.format(name=name, value=value, required=required, classes=classes, style=style)
def r_field_file(self, name, required=False, classes=[]): def r_field_file(self, name, required=False, classes=[], style=""):
tpl = self.field_tpl['file'] tpl = self.field_tpl['file']
return tpl.format(name=name, required=required, classes=classes) return tpl.format(name=name, required=required, classes=classes, style=style)
def r_field_password(self, name, value, min=None, required=False, classes=[]): def r_field_password(self, name, value, min=None, required=False, classes=[], style=""):
tpl = self.field_tpl['password'] tpl = self.field_tpl['password']
return tpl.format(name=name, value=value, min=min, required=required, classes=classes) return tpl.format(name=name, value=value, min=min, required=required, classes=classes, style=style)
def r_field_text(self, name, value, rows=4, cols=80, required=False, classes=[]): def r_field_text(self, name, value, rows=4, cols=80, required=False, classes=[], style=""):
tpl = self.field_tpl['text'] tpl = self.field_tpl['text']
return tpl.format(name=name, value=value, rows=rows, cols=cols, required=required, classes=classes) return tpl.format(name=name, value=value, rows=rows, cols=cols, required=required, classes=classes, style=style)
def r_field_radio(self, name, value, options, classes=[]): def r_field_radio(self, name, value, options, classes=[], style=""):
tpl_option = self.field_tpl['radio_option'] tpl_option = self.field_tpl['radio_option']
radio_elems = [] radio_elems = []
for o_value, o_label in options: for o_value, o_label in options:
checked = '' checked = ''
if o_value == value: if o_value == value:
checked = 'checked' checked = 'checked'
radio_elems.append(tpl_option.format(name=name, value=value, checked=checked, label=o_label, classes=classes)) radio_elems.append(tpl_option.format(name=name, value=value, checked=checked, label=o_label, classes=classes, style=style))
return u''.join(radio_elems) return u''.join(radio_elems)
def r_field_checkbox(self, name, checked, classes=''): def r_field_checkbox(self, name, checked, classes='', style=""):
tpl = self.field_tpl['checkbox'] tpl = self.field_tpl['checkbox']
return tpl.format(name=name, checked=checked, classes=classes) return tpl.format(name=name, checked=checked, classes=classes, style=style)
def r_field_select(self, name, value, options, classes=[]): def r_field_select(self, name, value, options, classes=[], style=""):
tpl_option = self.field_tpl['select_option'] tpl_option = self.field_tpl['select_option']
select_elems = [] select_elems = []
for o_value, o_label in options: for o_value, o_label in options:
selected = '' selected = ''
if o_value == value: if o_value == value:
selected = 'selected' selected = 'selected'
select_elems.append(tpl_option.format(value=o_value, selected=selected, label=o_label)) select_elems.append(tpl_option.format(value=o_value, selected=selected, label=o_label, style=style))
tpl = self.field_tpl['select'] tpl = self.field_tpl['select']
return tpl.format(name=name, select_elems=''.join(select_elems), classes=classes) return tpl.format(name=name, select_elems=''.join(select_elems), classes=classes, style=style)
def r_form_line(self, type, title, input, classes, errors): def r_form_line(self, type, title, input, classes, errors):
if type == 'checkbox': if type == 'checkbox':
@ -849,6 +849,8 @@ class ScriptFormWebApp(WebAppHandler):
if field.get('hidden', None): if field.get('hidden', None):
params['classes'].append('hidden') params['classes'].append('hidden')
params["style"] = field.get("style", "")
if field['type'] not in ('file', 'checkbox'): if field['type'] not in ('file', 'checkbox'):
params['value'] = form_values.get(field['name'], '') params['value'] = form_values.get(field['name'], '')
@ -1023,7 +1025,6 @@ class ScriptFormWebApp(WebAppHandler):
return return
path = os.path.join(form_config.static_dir, fname) path = os.path.join(form_config.static_dir, fname)
print path
if not os.path.exists(path): if not os.path.exists(path):
self.send_error(404, "Not found") self.send_error(404, "Not found")
return return

Loading…
Cancel
Save