Opened 6 years ago

Closed 3 years ago

Last modified 3 years ago

#15511 closed New feature (fixed)

MultiValueField should allow subfield requirement

Reported by: Jeremy Dunck Owned by: nobody
Component: Forms Version: master
Severity: Normal Keywords:
Cc: timograham@… Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

Example:

class PhoneFIeld(MultiValueField):
    def __init__(self, **kwargs):
        super(PhoneField).__init__(self, fields=(
            USPhoneNumberField(required=True),
            forms.CharField(error_messages={'invalid': u'Extension must be numeric'},
                required=False),
        ), required=True)

This would require a change to .clean so that all subfields which were marked required to be filled in, but any non-required subfield would not be required.

Currently, subfield required's are forced to False, and a required at the MultiValueField level means all subfields are required.

Change History (12)

comment:1 Changed 6 years ago by Russell Keith-Magee

Triage Stage: UnreviewedAccepted

comment:2 Changed 5 years ago by Łukasz Rekucki

Severity: Normal
Type: New feature

comment:3 Changed 5 years ago by Aymeric Augustin

UI/UX: unset

Change UI/UX from NULL to False.

comment:4 Changed 5 years ago by Aymeric Augustin

Easy pickings: unset

Change Easy pickings from NULL to False.

comment:5 Changed 3 years ago by Tai Lee

Has patch: set
Version: 1.2master

I have opened a PR with backwards compatible fix, tests and docs.

https://github.com/django/django/pull/1048

comment:6 Changed 3 years ago by Tai Lee

This also refs #13342 which was closed as wontfix by russellm who accepted this ticket. Presumably this ticket now includes sufficient detail.

comment:7 Changed 3 years ago by Tim Graham

Cc: timograham@… added
Patch needs improvement: set

PR needs to be rebased to master.

comment:8 Changed 3 years ago by Tai Lee

Patch needs improvement: unset

Rebased to master.

comment:9 Changed 3 years ago by Tim Graham <timograham@…>

Resolution: fixed
Status: newclosed

In 12806758347dfd63a3cd1bfc0d925c09fdbd9cff:

Fixed #15511 -- Allow optional fields on MultiValueField` subclasses.

The MultiValueField class gets a new require_all_fields argument that
defaults to True. If set to False, individual fields can be made
optional, and a new incomplete validation error will be raised if any
required fields have empty values.

The incomplete error message can be defined on a MultiValueField
subclass or on each individual field. Skip duplicate errors.

comment:10 Changed 3 years ago by Nathan Osman

Just a quick clarification: will this fix make it into the final Django 1.6 release?

comment:11 Changed 3 years ago by Tim Graham

No, if you look at the commit you'll see it's been added to the release notes for 1.7.

comment:12 Changed 3 years ago by Nathan Osman

After a significant amount of effort, I have succeeded in backporting this fix to Django 1.5.2. Unfortunately, it wasn't possible to backport this fix (#15511) without also backporting #17051. So, here is a patch that combines the fix for both bugs:

https://gist.github.com/nathan-osman/6347178

I ran the test suite after applying the patch and it seemed to pass without any failures.

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