Reputation: 83
I'm new to WTForms. I wanted to create a new type of field called DollarField which would allow "$" and commas. I would then strip these out and save the value.
I did this which strips off the "$" and removes commas but now all the normal validation that works for DecimalField (e.g. catching cases if the user types in "asda") don't work.
class DollarField(DecimalField):
def process_formdata(self, valuelist):
if len(valuelist) == 1:
self.data = [valuelist[0].strip('$').replace(',', '')]
else:
self.data = []
Upvotes: 2
Views: 2621
Reputation: 43091
Your issue is that because you override process_formdata
, you are replacing the default processing code (which attempts to convert the string into a decimal, and thus raises an error if it cannot) with your own code. You'll want to make sure that your implementation of process_formdata
manually calls the parent's process_formdata
method so that its logic is also run, like so...
class DollarField(DecimalField):
def process_formdata(self, valuelist):
if len(valuelist) == 1:
self.data = [valuelist[0].strip('$').replace(',', '')]
else:
self.data = []
# Calls "process_formdata" on the parent types of "DollarField",
# which includes "DecimalField"
super(DollarField).process_formdata(self.data)
Upvotes: 2