When saving a RadioField to the DB, check that what is being saved is one of the options
This commit is contained in:
parent
a2754d0646
commit
ed20c6a6a2
@ -18,11 +18,16 @@ class BaseForm(FlaskForm):
|
|||||||
def data(self):
|
def data(self):
|
||||||
# remove 'csrf_token' key/value pair
|
# remove 'csrf_token' key/value pair
|
||||||
# remove empty strings and None from list fields
|
# remove empty strings and None from list fields
|
||||||
|
# prevent values that are not an option in a RadioField from being saved to the DB
|
||||||
_data = super().data
|
_data = super().data
|
||||||
_data.pop("csrf_token", None)
|
_data.pop("csrf_token", None)
|
||||||
for field in _data:
|
for field in _data:
|
||||||
if _data[field].__class__.__name__ == "list":
|
if _data[field].__class__.__name__ == "list":
|
||||||
_data[field] = [el for el in _data[field] if el not in EMPTY_LIST_FIELD]
|
_data[field] = [el for el in _data[field] if el not in EMPTY_LIST_FIELD]
|
||||||
|
if self[field].__class__.__name__ == "RadioField":
|
||||||
|
choices = [el[0] for el in self[field].choices]
|
||||||
|
if _data[field] not in choices:
|
||||||
|
_data[field] = None
|
||||||
return _data
|
return _data
|
||||||
|
|
||||||
def validate(self, *args, **kwargs):
|
def validate(self, *args, **kwargs):
|
||||||
|
Loading…
x
Reference in New Issue
Block a user