Opened 15 years ago

Closed 4 years ago

#13060 closed Cleanup/optimization (fixed)

ManagementForm exception in case of bad prefix should be easier to understand

Reported by: Karel Owned by: Manav Agarwal
Component: Forms Version: dev
Severity: Normal Keywords: formset, ValidationError, ManagementForm
Cc: shawntherrien@… Triage Stage: Ready for checkin
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: yes UI/UX: no

Description

If user adds multiple formsets with prefixes, and specifies the prefix incorrectly when binding the form and validating:

some_formset = SomeFormSet(request.POST, 'articles')

instead of:

some_formset = SomeFormSet(request.POST, prefix='articles') 

Django "suppresses" the original exception and raises only relatively unhelpful "ManagementForm data is missing or has been tampered with".

In file django/forms/formsets.py, line 57:

  54. if self.data or self.files:
  55.     form = ManagementForm(self.data, auto_id=self.auto_id, prefix=self.prefix)
  56.     if not form.is_valid():
  57.         raise ValidationError('ManagementForm data is missing or has been tampered with') 

Suggestion: include form._errors in output, because for such a small bug in code, it can take a really long time find it.

{'INITIAL_FORMS': [u'This field is required.'],
 'MAX_NUM_FORMS': [u'This field is required.'],
 'TOTAL_FORMS': [u'This field is required.']}

Attachments (1)

formsets.diff (710 bytes ) - added by stherrien 15 years ago.
validation error with _errors included

Download all attachments as: .zip

Change History (16)

by stherrien, 15 years ago

Attachment: formsets.diff added

validation error with _errors included

comment:1 by anonymous, 15 years ago

Cc: shawntherrien@… added
Has patch: set

comment:2 by Russell Keith-Magee, 15 years ago

Triage Stage: UnreviewedAccepted

Not sure I completely agree with the suggested fix, but yes -- the error on failure of the management form could be a little more helpful, at the very least naming the field that is missing or invalid.

comment:3 by Luke Plant, 14 years ago

Type: New feature

comment:4 by Luke Plant, 14 years ago

Severity: Normal

comment:5 by Julien Phalip, 14 years ago

Needs tests: set
Patch needs improvement: set
Type: New featureCleanup/optimization

Patch would need improvement as per Russell's comment. Also needs tests.

comment:6 by Aymeric Augustin, 13 years ago

UI/UX: unset

Change UI/UX from NULL to False.

comment:7 by Aymeric Augustin, 13 years ago

Easy pickings: unset

Change Easy pickings from NULL to False.

comment:8 by Mariusz Felisiak, 4 years ago

Easy pickings: set
Has patch: unset
Needs tests: unset
Patch needs improvement: unset
Version: master

comment:9 by Manav Agarwal, 4 years ago

Owner: changed from nobody to Manav Agarwal
Status: newassigned

comment:10 by Manav Agarwal, 4 years ago

We may change the code from :

raise ValidationError(
                    _('ManagementForm data is missing or has been tampered with'),
                    code='missing_management_form',
                )

to something like:

raise ValidationError(
                    _('ManagementForm data is missing or has been tampered with %s' % form._errors),
                    code='missing_management_form',
                )

Approvals please.

comment:11 by Jacob Walls, 4 years ago

Has patch: set
Needs tests: set

comment:12 by Jacob Walls, 4 years ago

Needs tests: unset

On second thought I suppose it’s a bit early for the needs tests flag since no one has supplied a review.

comment:13 by Mariusz Felisiak, 4 years ago

Needs tests: set
Patch needs improvement: set

comment:14 by Mariusz Felisiak, 4 years ago

Needs tests: unset
Patch needs improvement: unset
Triage Stage: AcceptedReady for checkin

comment:15 by Mariusz Felisiak <felisiak.mariusz@…>, 4 years ago

Resolution: fixed
Status: assignedclosed

In 096b14f0:

Fixed #13060 -- Improved error message when ManagementForm data is missing.

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