#36618: Forms.add_error() misleading exception message
------------------------+-----------------------------------------
Reporter: UEWBot | Type: Uncategorized
Status: new | Component: Uncategorized
Version: 5.2 | Severity: Normal
Keywords: | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
------------------------+-----------------------------------------
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.
--
Ticket URL: <
https://code.djangoproject.com/ticket/36618>
Django <
https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.