Opened 2 hours ago

#36618 new Uncategorized

Forms.add_error() misleading exception message

Reported by: Chris Brand Owned by:
Component: Uncategorized Version: 5.2
Severity: Normal Keywords:
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

This code creates a form and attempts to add an error for a single field to it:

from django import forms
from django.core.exceptions import ValidationError

class FooForm(forms.Form):
    the_field = forms.CharField(max_length=100)

exc = ValidationError({'the_field': 'Something is wrong with the field'})
form = FooForm()

form.add_error('the_field', exc)

It results in the following error message:

TypeError: The argument `field` must be `None` when the `error` argument contains errors for multiple fields.

The reference to "errors for multiple fields" is just wrong - the error in question is for a single field.

For background, in my case the ValidationError is raised by my custom Model.clean(). What I really want to do it to map it from the Model field to the form field (which in general may not have the same name). The error message was quite confusing.

Change History (0)

Note: See TracTickets for help on using tickets.
Back to Top