-
Notifications
You must be signed in to change notification settings - Fork 28
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
Handling an empty / None option in selects. #64
Comments
The following modification to the commented code allows the rendering of the select. Not sure whether this is adequate:
This is a way to render such an option, but when the form is submitted, how should be handled this option if it is the selected one? I only get validation error when this is the case. |
Digging a bit on the code, I found that one can write custom
Not sure anymore whether this should be in the realm of WTForms-Components or in the realm of WTForms-Alchemy, but I think that somehow there should be an easier way to allow |
Last week I reported an issue into wtforms-alchemy regarding its inability to render a CountryField with an empty option for those cases when the user does not want to select one and None should be stored. But it seems to me that the place to report it might be this project because it seems that the real issue is how wtforms-components should render a None option for selects.
On the issue already mentinoed I tried to add an empty option as you see in the example that I provided. On the following different case, with different data type, the issue is just the same:
Note that I'm using sqlalchemy-utils and this model is translated to a form, being
guid
a select with three options. At the time of rendering the form, this fails because wtforms-components tries to coerce None into int, which causes an exception on line 270.wtforms-components/wtforms_components/widgets.py
Lines 262 to 270 in 523caeb
Trying options based on strings raises a different exception on the same line because it doesn't know how to covnert the option into a int. Supose that che choice is
('', '')
or('None', '')
, in any of both cases the problem is that it is unable to convert the option into an int:Would it be a reasonable solution to test whether the given
value is None
, and if so, do not perform the data coercion? I'm happy to help if this approach seems reasonable.The text was updated successfully, but these errors were encountered: