ModelForm "error_css_class" not getting applied to ValidationErrors raised in model.clean()

437 views
Skip to first unread message

Jacob Greene

unread,
Jul 15, 2019, 10:22:35 PM7/15/19
to Django users
Hello! Has anyone dealt with this before? It seems that my forms don't add the CSS class to errors raised in model.clean() or model.clean_fields() methods. 

I have a form that looks something like this: 
class HttpsFaxBoxForm(forms.ModelForm):
    error_css_class = 'form_error'

class Meta:
model = FaxBox
fields = ('outbound_cid_profile', 'id')


And I run validation in my models(keep the admin page consistent) like this:

class FaxBox(models.Model):
    something = fields.CharField(max_length=100)

    def clean(self):
if self.something == "shouldn't be here":
            raise ValidationError('This should not be here! Please fix!')
    super().clean_fields(exclude=None)

But when this exception gets raised, the CSS class isn't applied to it in the template. Anyone know of a way to work around this? Thanks for reading!

Jani Tiainen

unread,
Jul 16, 2019, 4:36:04 AM7/16/19
to django...@googlegroups.com
At least you should't call super.clean_fields() in clean() method... 

Also how are you outputting errors in your template?


--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/8cfa5a86-ccaf-47f7-8b10-93fbaaaf4386%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Jacob Greene

unread,
Jul 16, 2019, 8:31:00 AM7/16/19
to Django users
Thank you for the reply! And good catch haha, I'm not actually. I editing these by hand so I wasn't posting some confusing telephony terminology. I'm displaying errors in my templates by marking them with that "form_error" css class. They are just red and bold, most errors are shown with the correct CSS class like my UniqueConstraints or any exceptions I throw in the modelForm clean() method, but any exceptions I throw in the Model.clean() or Model.full_clean() do not get the correct css class.

Jani Tiainen

unread,
Jul 16, 2019, 9:45:51 AM7/16/19
to django...@googlegroups.com
Full_clean is Django internal method which you should't touch.

.clean() method validation errors go to special non-field errors and by quick look it should get correct css class.

If you could provide minimal project with suspected faulty behavior (for example in github) it would be easier to see if there is something wrong there.

ti 16. heinäk. 2019 klo 15.32 Jacob Greene <jacobgr...@gmail.com> kirjoitti:
Thank you for the reply! And good catch haha, I'm not actually. I editing these by hand so I wasn't posting some confusing telephony terminology. I'm displaying errors in my templates by marking them with that "form_error" css class. They are just red and bold, most errors are shown with the correct CSS class like my UniqueConstraints or any exceptions I throw in the modelForm clean() method, but any exceptions I throw in the Model.clean() or Model.full_clean() do not get the correct css class.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.

Melvyn Sopacua

unread,
Jul 16, 2019, 7:13:07 PM7/16/19
to django...@googlegroups.com
On dinsdag 16 juli 2019 03:25:11 CEST Jacob Greene wrote:

> Hello! Has anyone dealt with this before? It seems that my forms don't add
> the CSS class to errors raised in model.clean() or model.clean_fields()
> methods.
>
> I have a form that looks something like this:
>
> class HttpsFaxBoxForm(forms.ModelForm):
> error_css_class = 'form_error'

>
> And I run validation in my models(keep the admin page consistent) like this:

But your form is valid, so why should it apply error classes?

If you want to keep the admin page consistent, use the same form for admin,
but your core problem is that you mix input validation (task of form) with
data consistency (task of model). Keep those separated as much as possible and
things will go smoother.
--
Melvyn Sopacua


Reply all
Reply to author
Forward
0 new messages