-
-
Notifications
You must be signed in to change notification settings - Fork 45
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
Non-nullable columns with a default value should be optional #47
Comments
This problem has been on the backburner for me for a little bit. Picked it back up today, and this hackaround seems to work: class CustomModelConverter(ModelConverter):
@converts("Boolean")
def conv_Boolean(self, field_args, **_):
"""Hack: Prevent a required checkbox from failing client-side validation when False.
"Unchecked" is a valid value: False!
"""
field_args["validators"] = []
return fields.BooleanField(**field_args) My database field is not nullable, and it updates to false when I submit an unchecked field generated from my model with this converter. I have a deadline today but I'd like to take a stab at upstreaming this.
I'm with you! Not as a front-end for a boolean database field at least, because it only lets you select one value. A required boolean field needs a way to submit false. If we display the checkbox as the input mechanism for this, unchecked must be false. A nullable boolean field, though, can have three values. A checkbox can't represent that. What happens in practice is unchecked means With a short discussion on the above I think I'd be comfortable trying to implement the decision. My best stab at a concise problem summary: For a |
@mfisher87, I think your comments about the checkbox summarize it well. I've been moving toward making fewer fields nullable (especially booleans), but that doesn't cover everyone's use case. And while it is true that a nullable boolean has three fields, how many people actually think of it as such? (Especially given Python's truthiness.) Representing boolean columns as something other than a checkbox could cause even more issues... I took a stab at fixing this via the existence of a Edit: One issue with the radio button, while it can start unchecked, once an option is checked, is there a way to uncheck it entirely? From the perspective of a form submission, if the field was rendered, there is no discernable difference between |
Yeah, there is no way to unselect a radio button without javascript that I know of. In my radio button suggestion I think I was imagining that there would be an explicit selection for |
I confirmed that the solution in #48 works correctly (it will save the unchecked/false state) when |
While switching to
wtforms-sqlalchemy
from another library I ran into an issue that we have columns withnullable=False
anddefault
value, which should be able to post, but the generated form had added a required validator to those fields so validation failed.Example of columns:
Validation result from submitting form:
The expected result is that those fields should not have been required so that form processing would continue. Do you see any issues with setting columns with a
default
toOptional
?Edit: It seems that a checkbox should never be required, right?
The text was updated successfully, but these errors were encountered: