From ed20c6a6a24ef59b077810f6a36471a50373f12c Mon Sep 17 00:00:00 2001 From: leigh-mil Date: Mon, 11 Mar 2019 15:05:14 -0400 Subject: [PATCH] When saving a RadioField to the DB, check that what is being saved is one of the options --- atst/forms/forms.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/atst/forms/forms.py b/atst/forms/forms.py index e3f2081f..31e62476 100644 --- a/atst/forms/forms.py +++ b/atst/forms/forms.py @@ -18,11 +18,16 @@ class BaseForm(FlaskForm): def data(self): # remove 'csrf_token' key/value pair # 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.pop("csrf_token", None) for field in _data: if _data[field].__class__.__name__ == "list": _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 def validate(self, *args, **kwargs):