Opened 18 years ago

Closed 18 years ago

#3479 closed (wontfix)

Passing external data to Form objects in newforms

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

Description

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 by Benjamin Slavin, 18 years ago

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 by Chris Beaven, 18 years ago

Triage Stage: UnreviewedDesign decision needed

Does look useful. What do you think, Adrian?

comment:3 by Marc Fargas <telenieko@…>, 18 years ago

For the record:
Discussion has been held: here

comment:4 by Adrian Holovaty, 18 years ago

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