Search code examples
pythondjangodjango-formsdjango-validationdjango-media

Adding css class to field on validation error in django


I am using Django's modelform and its really good. How can I highlight the actual text box (e.g. border:red ) if there is a validation error associated with it. Basically what i want is to add a class (error) if there is a validation error to a field.


Solution

  • What about defining error_css_class? http://docs.djangoproject.com/en/stable/ref/forms/api/#styling-required-or-erroneous-form-rows?

    class MyForm(ModelForm):
        error_css_class = 'error'