Skip to content

Instantly share code, notes, and snippets.

@tysonholub
Last active November 20, 2018 19:41
Show Gist options
  • Save tysonholub/86a89b340a96f7893a5e92b28a4ee693 to your computer and use it in GitHub Desktop.
Save tysonholub/86a89b340a96f7893a5e92b28a4ee693 to your computer and use it in GitHub Desktop.
Flashes an error message for Hidden/CSRFToken fields on form submit
class HiddenValidationMixin(wtf_Form):
def validate_on_submit(self):
valid = super(wtf_Form, self).validate_on_submit()
if not valid and request.method == 'POST':
for k, v in self.errors.iteritems():
if k not in self.data:
flash('{0}: {1}'.format(k, v), 'error')
else:
attr = self.__getattribute__(k)
if attr and hasattr(attr, 'type'):
if attr.type in ['CSRFTokenField', 'HiddenField']:
flash('{0}: {1}'.format(k, v), 'error')
return valid
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment