Code

Opened 2 years ago

Closed 2 years ago

#17325 closed New feature (wontfix)

Add **kwargs to is_valid and several validation methods

Reported by: jorgeecardona Owned by: nobody
Component: Forms Version: 1.3
Severity: Normal 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 (last modified by aaugustin)

I like to use forms a lot, but sometimes I have to overwrite __init__ to accept some parameters that change the behaviour of the validation, I was thinking that maybe is better just to add a way to pass some data to the validation methods of the form in order to validate it.

Imagine that I want to create a form to change the email of an user and I don't want to use an argument in the request for "username" and use the username from the session framework (I just want to avoid to send the username in the request), I will have to overwrite the __init__ of the form in order to accept "user" as an argument and store it in self.user to be used later in clean_email. Instead, if we have this new argument in is_valid is just to do: form.is_valid(user=user).

This new argument doesn't hurt anyone, and It will help a lot to some of us that use form in this way.

Attachments (0)

Change History (3)

comment:1 Changed 2 years ago by aaugustin

  • Description modified (diff)
  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset

comment:2 Changed 2 years ago by aaugustin

  • Component changed from Uncategorized to Forms
  • Triage Stage changed from Unreviewed to Design decision needed

If I understand correctly, you'd want Form.is_valid to accept any keyword arguments and pass them to Form.full_clean.

I'm not convinced it's a good idea, because it's implicit, while passing arguments to the constructor is explicit — everything is visible in your Form subclass.

Furthermore, that would be difficult to achieve with the current implementation: Form.is_valid accesses the property Form.errors, which triggers the call to Form.full_clean. But you can't pass arguments to a property. So it isn't obvious that this change can be done without breaking backwards compatibility.

comment:3 Changed 2 years ago by Alex

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

I'm wontfixing this, adding extra data to a form is properly done in __init__ as aaugustin notes, it's not even possible with the data path presently in place to do what this ticket suggests.

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.