Opened 11 years ago

Closed 11 years ago

#3479 closed (wontfix)

Passing external data to Form objects in newforms

Reported by: Benjamin Slavin Owned by: Adrian Holovaty
Component: Forms Version: master
Severity: Keywords:
Cc: Triage Stage: Design decision needed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: UI/UX:


Forms may require external context when performing validation.

Consider, for example, a 'change password' form.
Validation of the current password is required to ensure correct authorization.
To validate the current password, the form needs to know the user it is validating against.

I am proposing an enhancement to newforms to allow this functionality... the 'extra' keyword argument for Form creation. This keyword is suggested to keep maintain similarity with 'extra_context' for generic views.

Form instantiation example:

PasswordChangeForm(request.POST.copy(), extra={'user': request.user})

Example of accessing this data from within the Form:

class PasswordChangeForm(forms.Form):
    current_password = forms.CharField(widget=forms.PasswordInput)
    def clean_current_password(self):
        u = self.extra.get('user', None)

This usage assumes that the passed argument is a dictionary, and that that dictionary will be stored in self.extra (e.g. self.extra['user']). It would also be possible to make anything passed in 'extra' an attribute of the form itself (e.g. self.user). I'm not particularly partial to either implementation.

Change History (4)

comment:1 Changed 11 years ago by Benjamin Slavin

Is there any interest in this feature? If so, I can provide a patch.

(I'll avoid triaging my own ticket, but this probably falls under 'design decision needed')

comment:2 Changed 11 years ago by Chris Beaven

Triage Stage: UnreviewedDesign decision needed

Does look useful. What do you think, Adrian?

comment:3 Changed 11 years ago by Marc Fargas <telenieko@…>

For the record:
Discussion has been held: here

comment:4 Changed 11 years ago by Adrian Holovaty

Resolution: wontfix
Status: newclosed

As Honza suggested in the mailing-list discussion, you're already able to do this in a *much* more generic fashion by simply subclassing the form and implementing init(). I'm marking the ticket as a wontfix.

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