1

In the wtform docuentation the following function is described as a widget that can render a SelectMultipleField as a collection of checkboxes:

def select_multi_checkbox(field, ul_class='', **kwargs): kwargs.setdefault('type', 'checkbox') field_id = kwargs.pop('id', field.id) html = [u'<ul %s>' % html_params(id=field_id, class_=ul_class)] for value, label, checked in field.iter_choices(): choice_id = u'%s-%s' % (field_id, value) options = dict(kwargs, name=field.name, value=value, id=choice_id) if checked: options['checked'] = 'checked' html.append(u'<li><input %s /> ' % html_params(**options)) html.append(u'<label for="%s">%s</label></li>' % (field_id, label)) html.append(u'</ul>') return u''.join(html) 

I am trying to actually use this as an example to see what it looks like in one of my forms. However I am having some trouble and I am not sure how to call it since I am only used to using the default fields. This is what I have tried:

class myForm(FlaskForm): my_choices = [('1', 'Choice1'), ('2', 'Choice2'), ('3', 'Choice3')] my_multi_select = SelectMultipleField(choices = my_choices,id='Test') my_checkbox_multi_select = select_multi_checkbox(my_multi_select) 

This gives me the following error:

line 52, in select_multi_checkbox field_id = kwargs.pop('id', field.id) AttributeError: 'UnboundField' object has no attribute 'id' 

My next iteration was this:

my_checkbox_multi_select = select_multi_checkbox(SelectMultipleField,ul_class='',choices=my_choices,id='test') 

This also gave me an error with the id attribute but the field wasn't Unbounded anymore.

line 52, in select_multi_checkbox field_id = kwargs.pop('id', field.id) AttributeError: type object 'SelectMultipleField' has no attribute 'id' 

I was wondering what the proper way to implement this is. I see that it is a function so I thought that it needed to be called on a field but I wasn't sure what I was doing wrong.

1 Answer 1

3

So I figured this out. I guess it was in the documentation but it was unclear to me for a while. Here is what I did to render this field. I guess this should have been obvious but if anyone else is looking for this here it is:

tester = SelectMultipleField(choices=my_choices, widget=select_multi_checkbox) 

Just call it as a widget. Simple enough. I wish I had put that together earlier.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.