Created
December 29, 2013 17:30
-
-
Save victorono/8172643 to your computer and use it in GitHub Desktop.
Apply error class to form elements django
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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