The WTForms documentation is woefully inadequate, they don\'t even show you one single example of a custom widget that isn\'t derived from another widget already.
I
You can take advantage of useful attributes on the field, namely 'description' and 'label' for this instance. This yields a much simpler setup:
from wtforms.widgets.core import HTMLString, html_params, escape
class InlineButtonWidget(object):
def __call__(self, field, **kwargs):
kwargs.setdefault('type', 'submit')
# Allow passing title= or alternately use field.description
title = kwargs.pop('title', field.description or '')
params = html_params(title=title, **kwargs)
html = '<button %s><span>%s</span></button>'
return HTMLString(html % (params, escape(field.label.text)))
Usage: (wrapped for readability)
class MyForm(Form):
foo = BooleanField(
u'Save',
description='Click here to save',
widget=InlineButtonWidget()
)
alternately, to have a field type for it:
class InlineButtonField(BooleanField):
widget = InlineButtonWidget()
class MyForm(Form):
foo = InlineButtonField('Save', description='Save Me')
Answered under Update, but needed this init inside Field derived class.
def __init__(self, label=None, validators=None, text='Save', **kwargs):
super(InlineButton, self).__init__(label, validators, **kwargs)
self.text = text