My Python Flask app is using WTForms with built in python Enum support. I'm attempting to submit a form (POST) where a SelectField is populated by all values of a Enum.
When I hit 'Submit' i'm given the error, 'Not a valid choice.' This seems strange because when checking the values of the incoming form, the form seemingly does contain a valid choice from the list of Enum values provided.
I'm using a subclass of Enum named AJBEnum
which is formatted like so:
class UserRole(AJBEnum):
admin = 0
recipient = 1
I chose to do this because I use many Enums through the project and wanted to write a helper function that gathers all choices and formats them WTForm SelectField tuple friendly. AJBEnum is formatted like so:
class AJBEnum(Enum):
@classmethod
def choices(cls, blank=True):
choices = []
if blank == True:
choices += [("", "")]
choices += [(choice, choice.desc()) for choice in cls]
return choices
Which means I can give WTForms all choices for UserRole
during the creating of the SelectField like so:
role = SelectField('Role', choices=UserRole.choices(blank=False), default=UserRole.recipient)
Note the function parameter blank
provides an additional blank SelectField option in case the SelectField is optional. In this case, it is not.
When I hit the Submit button I check the incoming incoming request in my routes and by printing the form.data
i'm given this content:
{'email': 'abc@gmail.com', 'password': 'fake', 'plan': 'A', 'confirm': 'fake', 'submit': True, 'id': None, 'role': 'UserRole.recipient'}
As you can see, it appears WTForms has stringified UserRole.recipient. Is there a way to coerce WTForms into converting the incoming POST request value back to the Enum value that it was intended to be?