Using Python 3.8 with WTForms == 3.0.1
It seems pretty clear the default input for BooleanField is broken
Doesn't matter what the default is, not having input for the field will result in False
From Field.process():
if formdata is not None:
if self.name in formdata:
self.raw_data = formdata.getlist(self.name)
else:
self.raw_data = []
try:
self.process_formdata(self.raw_data) #< With no input this wil be []
In Boolean Field
def process_formdata(self, valuelist):
if not valuelist or valuelist[0] in self.false_values:
self.data = False #NO INPUT ENDS UP HERE!
else:
self.data = True
Looking at the tests these seem broken too:
def test_defaults(): #SHOWN FOR REFERENCE
# Test with no post data to make sure defaults work
form = BoringForm()
assert form.bool1.raw_data is None
assert form.bool1.data is False
assert form.bool2.data is True
def test_with_postdata():
form = BoringForm(DummyPostData(bool1=["a"]))
assert form.bool1.raw_data == ["a"]
assert form.bool1.data is True #Why! a isn't boolean
form = BoringForm(DummyPostData(bool1=["false"], bool2=["false"]))
assert form.bool1.data is False
assert form.bool2.data is True #WTF
Its clear to see that whenever the value isn't input it will be false.
The simplest change could be:
def process_formdata(self, valuelist):
if len(valuelist)==0:
self.data = self.default_value
elif valuelist[0] in self.false_values:
self.data = False
else:
self.data = True
But to prevent any ambiguity in form parsing this is how BooleanField should work:
Input:
BooleanField( <normal args>, default:bool = False, true_values=None, false_values=None)
Parsing:
def process_formdata(self, valuelist):
if len(valuelist)==0:
self.data = self.default_value
elif valuelist[0] in self.false_values:
self.data = False
elif valuelist[0] in self.true_values:
self.data = True
else:
#Print warning!
self.data = self.default_value
Using Python 3.8 with WTForms == 3.0.1
It seems pretty clear the default input for BooleanField is broken
Doesn't matter what the default is, not having input for the field will result in False
From Field.process():
In Boolean Field
Looking at the tests these seem broken too:
Its clear to see that whenever the value isn't input it will be false.
The simplest change could be:
But to prevent any ambiguity in form parsing this is how BooleanField should work:
Input:
Parsing: