Skip to content
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

Merged
merged 5 commits into from
Jul 22, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 13 additions & 0 deletions docs/fields.rst
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,19 @@ refer to a single input from the form.
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(Form):
my_select_field = SelectField('Select an option', choices=[('1', 'Option 1'), ('2', 'Option 2'), ('None', 'No option')], 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**::

class DynamicSelectForm(Form):
Expand Down