Skip to content

Instantly share code, notes, and snippets.

@doobeh
Created January 29, 2013 15:54
Show Gist options
  • Select an option

  • Save doobeh/4665281 to your computer and use it in GitHub Desktop.

Select an option

Save doobeh/4665281 to your computer and use it in GitHub Desktop.
Multiple Checkboxes
from flask.ext.wtf import Form, TextField, Required, TextAreaField,\
IntegerField, DecimalField, SubmitField, SelectField, HiddenField, FileField,\
QuerySelectMultipleField, widgets
class ToggleWidget(object):
"""
Renders a list of fields as a `ul` or `ol` list.
This is used for fields which encapsulate many inner fields as subfields.
The widget will try to iterate the field to get access to the subfields and
call them to render them.
If `prefix_label` is set, the subfield's label is printed before the field,
otherwise afterwards. The latter is useful for iterating radios or
checkboxes.
"""
def __init__(self, prefix_label=True):
self.prefix_label = prefix_label
def __call__(self, field, **kwargs):
kwargs.setdefault('id', field.id)
html = ['']
for subfield in field:
html.append('<div class="row toggle">')
if self.prefix_label:
html.append('%s' % (subfield()))
#html.append('<li>%s: %s</li>' % (subfield.label, subfield()))
else:
html.append('<li>%s %s</li>' % (subfield(), subfield.label))
html.append('</div>')
return HTMLString(''.join(html))
class MultiQuerySelectField(QuerySelectMultipleField):
widget = ToggleWidget()
option_widget = widgets.CheckboxInput()
def all_amenities():
return Amenity.query.order_by(Amenity.name.asc()).all()
class AmenitiesAddForm(Form):
amenities = MultiQuerySelectField(query_factory=all_amenities, allow_blank=True)
submit = SubmitField('Finish!')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment