-
Notifications
You must be signed in to change notification settings - Fork 396
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Coerce function example #789
Changes from 3 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -313,6 +313,20 @@ refer to a single input from the form. | |
`coerce` keyword arg to :class:`~wtforms.fields.SelectField` says that we | ||
use :func:`int()` to coerce form data. The default coerce is | ||
:func:`str()`. | ||
|
||
**Coerce function example**:: | ||
|
||
def coerce_none(value): | ||
if value == 'None': | ||
return None | ||
return value | ||
|
||
class NonePossible(From): | ||
choices = [('1', 'Option 1'), ('2', 'Option 2'), ('None', 'No option')] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This shouldn't be a class variable, inline this in the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I thought it would improve readability. Thanks for the feedback, I have adjusted this in the new commit. |
||
my_select_field = SelectField('Select an option', choices=choices, coerce=coerce_none) | ||
|
||
Note when the option None is selected a 'None' str will be passed. By using a coerce | ||
function the 'None' str will be converted to None. | ||
|
||
**Skipping choice validation**:: | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Typo, this should be
Form