Opened 13 years ago

Closed 13 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: dev
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 13 years ago.
Isolate validators list between form instances

Download all attachments as: .zip

Change History (4)

by Claude Paroz, 13 years ago

Attachment: 17127-1.diff added

Isolate validators list between form instances

comment:1 by Claude Paroz, 13 years ago

Has patch: set

comment:2 by Carl Meyer, 13 years ago

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 by Carl Meyer, 13 years ago

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