Opened 14 years ago

Closed 13 years ago

#14184 closed Bug (fixed)

Validators not called on MultiValueField

Reported by: anonymous Owned by: Paul Collins
Component: Forms Version: 1.2
Severity: Normal Keywords: MultiValueField Validators
Cc: Paul Collins Triage Stage: Ready for checkin
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

Validators are not called for the compressed value of a MultiValueField.

If, say, I have a MultiValueField subclass which takes a first name and a last name and compresses the two into a "Lastname, Firstname" and this MultiValueField subclass was instantiated using validators = [disallow_john_doe] , one would expect that disallow_john_doe(compressed value) would be executed. However, this is not the case.

Looking at the source, it appears that while the clean() method of django.forms.fields.Field calls run_validators() , the clean() method of django.forms.fields.MultiValueField does not. This is different from expected behaviour and is not documented to my knowledge.

The workaround is to implement validate() in the subclass.

Attachments (2)

add_run_validators_call.patch (2.2 KB ) - added by Paul Collins 13 years ago.
add_run_validators_call.2.patch (2.0 KB ) - added by Paul Collins 13 years ago.

Download all attachments as: .zip

Change History (8)

comment:1 by Daniel F Moisset, 14 years ago

Triage Stage: UnreviewedAccepted

Either the documentation or MultiValueField are wrong. (I'd say that MultiValueField is). Accepting

comment:2 by Julien Phalip, 13 years ago

Severity: Normal
Type: Bug

by Paul Collins, 13 years ago

comment:3 by Paul Collins, 13 years ago

Cc: Paul Collins added
Easy pickings: unset
Has patch: set
Owner: changed from nobody to Paul Collins
UI/UX: unset

Was a bit surprised by this one myself. I'm working on the idea that the MultiValueField is wrong rather than the docs since the validators arg is a core Field arg. Patch includes a regression test for this and a fix which appears to resolve the issue.

On a related note MultiValueField.validate is overridden to be a noop, but the parent class Field checks if the field is required. I've left it alone but, should MultiValueField.validate be removed so that the Field.validate gets called instead?

Last edited 13 years ago by Paul Collins (previous) (diff)

by Paul Collins, 13 years ago

in reply to:  3 comment:4 by Paul Collins, 13 years ago

Replying to paulcollins:

On a related note MultiValueField.validate is overridden to be a noop, but the parent class Field checks if the field is required. I've left it alone but, should MultiValueField.validate be removed so that the Field.validate gets called instead?

Never mind, eventually rereading the clean function for the MultiValueField answered my question. Seems the clean method already does the work of the Field.validate method.

comment:5 by Claude Paroz, 13 years ago

Triage Stage: AcceptedReady for checkin

comment:6 by Jannis Leidel, 13 years ago

Resolution: fixed
Status: newclosed

In [17430]:

Fixed #14184 -- Enabled running the validators in MultiValueFields. Thanks, paulcollins.

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