Skip to content

Instantly share code, notes, and snippets.

@victorono
Created December 29, 2013 17:30
Show Gist options
  • Save victorono/8172643 to your computer and use it in GitHub Desktop.
Save victorono/8172643 to your computer and use it in GitHub Desktop.
Apply error class to form elements django
class MyForm(forms.Form):
"""
Extend from this form so your widgets have an 'error' class if there's
an error on the field.
"""
def __init__(self, *args, **kwargs):
super(MyForm, self).__init__(*args, **kwargs)
if self.errors:
for f_name in self.fields:
if f_name in self.errors:
classes = self.fields[f_name].widget.attrs.get('class', '')
classes += ' my_error_class'
self.fields[f_name].widget.attrs['class'] = classes
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment