Flask-WTF uses input=submit instead of button type=submit

Dexter picture Dexter · Jan 20, 2013 · Viewed 7.3k times · Source

I would like Flask's "SubmitField" to use

<button type="submit" title="Save this form"><span>Save</span></button>

Instead of:

<input type="submit" title="Save this form" />

I am printing it out in templates:

{{ field(class=css_class, title=field.description, **kwargs) }}

I'm guessing that I have to somehow modify SubmitInput (the widget behind SubmitField), but I'm not sure how to do that exactly, do I somehow have to modify __html__()?

EDIT:

from flask.ext.wtf import Required, Length, EqualTo, Field, TextInput
from flask import Markup
class InlineButtonWidget(object):
    html = """
    <button type="submit" title="%s"><span>%s</span></button>
    """

    def __init__(self, input_type='submit'):
        self.input_type = input_type

    def __call__(self, field, **kwargs):
        kwargs.setdefault('id', field.id)
        kwargs.setdefault('type', self.input_type)
        if 'value' not in kwargs:
            kwargs['value'] = field._value()
        return HTMLString(self.html % (field.name, field.label ))


class InlineButton(Field):
  widget = InlineButtonWidget()
  def _value(self):
    if self.data:
        return u', '.join(self.data)
    else:
        return u''


class SignupForm(Form):
    name = TextField('Name', [Length(min=1, max=200)])
    submit = InlineButton('sign up')

I want to be able to do this:

submit = InlineButton({'name' : 'submit', 'title' : 'sign up today for prizes.', 'type' : 'submitfieldtype', 'textInsideSpan' : 'Save Current Work' })

Answer

Mohammad Efazati picture Mohammad Efazati · Jan 21, 2013

its easy, you can create new widget like InlineButtonWidget() but i think better remove submit from form and use this in template

<button type="submit" title="Save this form"><span>Save</span></button>

of cource you can work with this too:

{% for name, label in buttons %}
    <button type="submit" title="{{name}}"><span>{{name}}</span></button>
{% endfor %}

sample widget:

class InlineButtonWidget(object):
    html = """
    <button type="submit" title="%s"><span>%s</span></button>
    """

    def __init__(self, input_type='submit'):
        self.input_type = input_type

    def __call__(self, field, **kwargs):
        kwargs.setdefault('id', field.id)
        kwargs.setdefault('type', self.input_type)
        if 'value' not in kwargs:
            kwargs['value'] = field._value()
        return HTMLString(self.html % (field.name, field.lable ))