Opened 3 years ago

Closed 3 years ago

#17127 closed Bug (fixed)

Validators list is shared among fields of different form instances

Reported by: claudep 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 claudep 3 years ago.
Isolate validators list between form instances

Download all attachments as: .zip

Change History (4)

Changed 3 years ago by claudep

Isolate validators list between form instances

comment:1 Changed 3 years ago by claudep

  • Has patch set
  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset

comment:2 Changed 3 years ago by carljm

  • Triage Stage changed from Unreviewed to Ready 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 3 years ago by carljm

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

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