Bootstrap 3 custom forms

146 views
Skip to first unread message

Jim S

unread,
Sep 18, 2014, 12:42:40 PM9/18/14
to web...@googlegroups.com
Hi

Using custom forms and trying to work with bootstrap three.

I'm using SQLFORM.grid() and then modifying the view using form.custom.begin.  The form tag in the html is still generated as:

<form method="post" enctype="multipart/form-data" class="web2py_form" action="#">

instead of the form tag that is generated when you use SQLFORM.grid without a custom view:

<form class="form-horizontal web2py_form col-md-7 col-lg-5 col-sm-9" method="post" enctype="multipart/form-data" action="#">

Here is the SQLFORM.grid() call:

    grid = SQLFORM.grid(query, fields=fields,
                       
orderby=orderby,
                        create
=create, details=details,
                        editable
=editable, deletable=deletable,
                        csv
=False, search_widget=defaultSearch,
                        paginate
=15, maxtextlength=45,
                        formname
='userForm',
                        formstyle
=my_formstyle, ui=grid_ui)

my_formstyle is:

def my_formstyle(form, fields):
    col_sm_label_size
= 4
    col_md_label_size
= 3
    form
.add_class('form-horizontal col-sm-9 col-md-7 col-lg-5')
    label_col_class
= "col-sm-%d col-md-%d" % (col_sm_label_size, col_md_label_size)
    col_class
= "col-sm-%d col-md-%d" % ((12 - col_sm_label_size), (12 - col_md_label_size))
    offset_class
= "col-sm-offset-%d col-md-offset-%s" % (col_sm_label_size, col_md_label_size)
    parent
= CAT()
   
for id, label, controls, help in fields:
       
# wrappers
        _help
= SPAN(help, _class='help-block')
       
# embed _help into _controls
        _controls
= DIV(controls, _help, _class=col_class)
       
if isinstance(controls, INPUT):
           
if controls['_type'] == 'submit':
                controls
.add_class('btn btn-primary')
                _controls
= DIV(controls, _class="%s %s" % (col_class, offset_class))
           
if controls['_type'] == 'button':
                controls
.add_class('btn btn-default')
           
elif controls['_type'] == 'file':
                controls
.add_class('input-file')
           
elif controls['_type'] == 'text':
                controls
.add_class('form-control')
           
elif controls['_type'] == 'email':
                controls
.add_class('form-control')
           
elif controls['_type'] == 'password':
                controls
.add_class('form-control')
           
elif controls['_type'] == 'checkbox':
                label
['_for'] = None
                label
.insert(0, controls)
                _controls
= DIV(DIV(label, _help, _class="checkbox"),
                            _class
="%s %s" % (offset_class, col_class))
                label
= ''
           
elif isinstance(controls, SELECT):
                controls
.add_class('form-control')
           
elif isinstance(controls, TEXTAREA):
                controls
.add_class('form-control')


       
elif isinstance(controls, SPAN):
            _controls
= P(controls.components, _class="form-control-static %s" % col_class)


       
if isinstance(label, LABEL):
            label
['_class'] = 'control-label %s' % label_col_class


        parent
.append(DIV(label, _controls, _class='form-group', _id=id))
   
return parent


and grid_ui is:

grid_ui = dict(widget='',
               header
='',
               content
='',
               
default='',
               cornerall
='',
               cornertop
='',
               cornerbottom
='',
               button
='btn btn-default button',
               buttontext
='buttontext button',
               buttonadd
='glyphicon glyphicon-plus space-after',
               buttonback
='glyphicon glyphicon-arrow-left space-after',
               buttonexport
='glyphicon glyphicon-download space-after',
               buttondelete
='glyphicon glyphicon-trash space-after',
               buttonedit
='glyphicon glyphicon-pencil space-after',
               buttontable
='glyphicon glyphicon-arrow-right space-after',
               buttonview
='glyphicon glyphicon-arrow-zoom-in space-after')


Any pointers on where to look to get the form.custom.begin to generate the same form tag as SQLFORM.grid()?

-Jim

Leonel Câmara

unread,
Sep 18, 2014, 1:31:52 PM9/18/14
to web...@googlegroups.com
You can't this is a limitation on the way form.custom.begin is implemented.

See:


Just don't use form.custom.begin and write the HTML directly.

Literally write this in your HTML instead of form.custom.begin.

<form class="form-horizontal web2py_form col-md-7 col-lg-5 col-sm-9" method="post" enctype="multipart/form-data" action="#">

Then continue your custom form as usual.

Jim Steil

unread,
Sep 18, 2014, 2:25:48 PM9/18/14
to web...@googlegroups.com
Thank you.  Will proceed as advised.

--
Resources:
- http://web2py.com
- http://web2py.com/book (Documentation)
- http://github.com/web2py/web2py (Source code)
- https://code.google.com/p/web2py/issues/list (Report Issues)
---
You received this message because you are subscribed to a topic in the Google Groups "web2py-users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/web2py/1yCGgKANssE/unsubscribe.
To unsubscribe from this group and all its topics, send an email to web2py+un...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages