Code

Opened 7 years ago

Closed 7 years ago

#3479 closed (wontfix)

Passing external data to Form objects in newforms

Reported by: Benjamin Slavin Owned by: adrian
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:

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.

Attachments (0)

Change History (4)

comment:1 Changed 7 years ago by Benjamin Slavin

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset

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 7 years ago by SmileyChris

  • Triage Stage changed from Unreviewed to Design decision needed

Does look useful. What do you think, Adrian?

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

For the record:
Discussion has been held: here

comment:4 Changed 7 years ago by adrian

  • Resolution set to wontfix
  • Status changed from new to closed

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.

Add Comment

Modify Ticket

Change Properties
<Author field>
Action
as closed
as The resolution will be set. Next status will be 'closed'
The resolution will be deleted. Next status will be 'new'
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.