BaseForm.add_error() misleading exception message when both field and dict provided
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
(14)
| Component: |
Uncategorized → Forms
|
| Easy pickings: |
set
|
| Summary: |
Forms.add_error() misleading exception message → BaseForm.add_error() misleading exception message when both field and dict provided
|
| Triage Stage: |
Unreviewed → Accepted
|
| Type: |
Uncategorized → Cleanup/optimization
|
| Component: |
Forms → Uncategorized
|
| Easy pickings: |
unset
|
| Summary: |
BaseForm.add_error() misleading exception message when both field and dict provided → Forms.add_error() misleading exception message
|
| Triage Stage: |
Accepted → Unreviewed
|
| Type: |
Cleanup/optimization → Uncategorized
|
| Owner: |
set to The5cheduler
|
| Status: |
new → assigned
|
| Component: |
Uncategorized → Testing framework
|
| Needs tests: |
set
|
| Component: |
Testing framework → Forms
|
| Easy pickings: |
set
|
| Needs tests: |
unset
|
| Patch needs improvement: |
set
|
| Summary: |
Forms.add_error() misleading exception message → BaseForm.add_error() misleading exception message when both field and dict provided
|
| Type: |
Uncategorized → Cleanup/optimization
|
| Triage Stage: |
Unreviewed → Accepted
|
| Owner: |
changed from The5cheduler to Nilesh Pahari
|
| Patch needs improvement: |
unset
|
| Triage Stage: |
Accepted → Ready for checkin
|
| Resolution: |
→ fixed
|
| Status: |
assigned → closed
|
Good spot, happy to take a PR replacing the reference to "multiple fields" with a mention of a dictionary instead. Incidentally this condition isn't tested, so we'll benefit from the addition of a test.