Opened 5 years ago

Closed 5 years ago

#17127 closed Bug (fixed)

Validators list is shared among fields of different form instances

Reported by: Claude Paroz Owned by: nobody
Component: Forms Version: master
Severity: Normal Keywords:
Cc: 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

When forms are created, fields are not new instances, but instead are a deepcopy of the Form class base_fields. If you modified afterwards a form field validators list, it is modified for all other form instances. Example:

from django import forms
from django.core import validators

class MyForm(forms.Form):
    myfield = forms.CharField(max_length=25)

f1 = MyForm()
f2 = MyForm()

f1.fields['myfield'].validators
>>> [<django.core.validators.MaxLengthValidator object at 0x32b53d0>]

f2.fields['myfield'].validators
>>> [<django.core.validators.MaxLengthValidator object at 0x32b53d0>]

f1.fields['myfield'].validators[0] = validators.MaxValueValidator(12)

f1.fields['myfield'].validators
>>> [<django.core.validators.MaxValueValidator object at 0x3205a90>]

f2.fields['myfield'].validators
>>> [<django.core.validators.MaxValueValidator object at 0x3205a90>]

This is a problem for me for example to properly resolve #17125.

Attachments (1)

17127-1.diff (1.5 KB) - added by Claude Paroz 5 years ago.
Isolate validators list between form instances

Download all attachments as: .zip

Change History (4)

Changed 5 years ago by Claude Paroz

Attachment: 17127-1.diff added

Isolate validators list between form instances

comment:1 Changed 5 years ago by Claude Paroz

Has patch: set

comment:2 Changed 5 years ago by Carl Meyer

Triage Stage: UnreviewedReady for checkin

Yes, this is a bug. Changing validators on a specific form's field instance should not change it for all instances. Patch looks good to me.

comment:3 Changed 5 years ago by Carl Meyer

Resolution: fixed
Status: newclosed

In [17046]:

Fixed #17127 -- Made field validators list independent per form instance. Thanks claudep for report and patch.

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