Skip to content

Implement ButtonField - #892

Merged
azmeuk merged 1 commit into
pallets-eco:mainfrom
azmeuk:784-button
May 7, 2026
Merged

azmeuk merged 1 commit into
pallets-eco:mainfrom
azmeuk:784-button

Conversation

@azmeuk

@azmeuk azmeuk commented May 3, 2026

Copy link
Copy Markdown
Member

Implement ButtonField inheriting StringField as an alternative to SubmitField that inherists BooleanField. It uses the <button type="submit"> tag, which allow the widget to have a label different than the value.

fixes #784

@azmeuk
azmeuk requested a review from ThiefMaster May 3, 2026 13:17
Comment thread src/wtforms/fields/simple.py Outdated

The field's label is also used as the rendered HTML ``value`` of the submit
control. Its WTForms data is boolean: the field is ``True`` when that
submit button was pressed, and ``False`` otherwise.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it actually False, or rather None?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

False, but maybe it worth mentioning that if the button is clicked but have an empty HTML value, then its wtforms value is False.

>>> from wtforms import Form, SubmitField
>>> from werkzeug.datastructures import MultiDict
>>> class F(Form):
...     submit = SubmitField("Save")
...     
>>> f = F(MultiDict({"submit": "Save"}))
>>> f.validate()
True
>>> f.submit.data
True
>>> f = F(MultiDict({"submit": ""}))
>>> f.validate()
True
>>> f.submit.data
False
>>> f = F(MultiDict({}))
>>> f.validate()
True
>>> f.submit.data
False

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

@azmeuk
azmeuk force-pushed the 784-button branch 2 times, most recently from f6c8274 to f2de571 Compare May 3, 2026 16:07
@azmeuk azmeuk added enhancement New feature, or existing feature improvement labels May 5, 2026
@azmeuk
azmeuk merged commit fca0d86 into pallets-eco:main May 7, 2026
17 checks passed
@azmeuk
azmeuk deleted the 784-button branch May 7, 2026 12:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature, or existing feature improvement

Development

Successfully merging this pull request may close these issues.

Button submit

2 participants