From 221fdc51ba5f1ca8a79ce9195d9239893155174e Mon Sep 17 00:00:00 2001 From: Jaripekkaf Date: Tue, 22 Oct 2013 13:15:21 +0300 Subject: [PATCH] bootstrap3 formstyle --- gluon/sqlhtml.py | 56 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) diff --git a/gluon/sqlhtml.py b/gluon/sqlhtml.py index cd473501..34e82a7a 100644 --- a/gluon/sqlhtml.py +++ b/gluon/sqlhtml.py @@ -797,6 +797,61 @@ def formstyle_bootstrap(form, fields): parent.append(DIV(label, _controls, _class='control-group', _id=id)) return parent +def formstyle_bootstrap3(form, fields): + ''' bootstrap 3 format form layout ''' + form.add_class('form-horizontal') + parent = FIELDSET() + for id, label, controls, help in fields: + # wrappers + _help = SPAN(help, _class='help-block') + # embed _help into _controls + _controls = DIV(controls, _help, _class='col-lg-4') + # submit unflag by default + _submit = False + if isinstance(controls, INPUT): + controls.add_class('col-lg-4') + + if controls['_type'] == 'submit': + # flag submit button + _submit = True + controls['_class'] = 'btn btn-primary' + if controls['_type'] == 'button': + controls['_class'] = 'btn btn-default' + elif controls['_type'] == 'file': + controls['_class'] = 'input-file' + elif controls['_type'] == 'text': + controls['_class'] = 'form-control' + elif controls['_type'] == 'password': + controls['_class'] = 'form-control' + elif controls['_type'] == 'checkbox': + controls['_class'] = 'checkbox' + + + + # For password fields, which are wrapped in a CAT object. + if isinstance(controls, CAT) and isinstance(controls[0], INPUT): + controls[0].add_class('col-lg-2') + + if isinstance(controls, SELECT): + controls.add_class('form-control') + + if isinstance(controls, TEXTAREA): + controls.add_class('form-control') + + if isinstance(label, LABEL): + label['_class'] = 'col-lg-2 control-label' + + + if _submit: + # submit button has unwrapped label and controls, different class + parent.append(DIV(label, DIV(controls,_class="col-lg-4 col-lg-offset-2"), _class='form-group', _id=id)) + # unflag submit (possible side effect) + _submit = False + else: + # unwrapped label + parent.append(DIV(label, _controls, _class='form-group', _id=id)) + return parent + class SQLFORM(FORM): @@ -875,6 +930,7 @@ class SQLFORM(FORM): divs=formstyle_divs, ul=formstyle_ul, bootstrap=formstyle_bootstrap, + bootstrap3=formstyle_bootstrap3, inline=formstyle_inline, ))