I am strugling to find resources that explain how to create custom widget with wtforms. I want to display a list of toggle buttons. So far I ve been able to create a list of checkboxes with
SelectMultipleFields: class PrecisionForm(FlaskForm): phone_number = StringField('Phone number', [validators.Length(min=4, max=25)]) contact_emails = SelectMultipleField( 'Contact emails', [at_least_one], choices=[], option_widget=widgets.CheckboxInput(), widget=widgets.ListWidget(prefix_label=False) ) but now I want to modify my widget.CheckBoxInput() with my custom widget looking like :
<label class='switch'> <input type='checkbox'><span class='slider round'></span></label>
I would be very grateful if someone could explain step by step how to do that.
--UPDATE--
I found a way to achieve what I wanted to do but it seems pretty bad :
class ToggleButtonWidget(object): def __call__(self, field): html = '<label class="switch"><input type="checkbox" id={} name={} value={}><span class="slider round"></span></label>'.format(field.id, field.name, field.label.__dict__['text']) return HTMLString(html) especially this part : field.label.__dict__['text'] that I use to set the value of my checkbox (the field object from wtforms has no attribute value). Is there a better way ?