Ticket #17127: 17127-1.diff

File 17127-1.diff, 1.5 KB (added by Claude Paroz, 12 years ago)

Isolate validators list between form instances

  • django/forms/fields.py

    diff --git a/django/forms/fields.py b/django/forms/fields.py
    index 8e727b4..0c598dd 100644
    a b class Field(object):  
    178178        result = copy.copy(self)
    179179        memo[id(self)] = result
    180180        result.widget = copy.deepcopy(self.widget, memo)
     181        result.validators = self.validators[:]
    181182        return result
    182183
    183184class CharField(Field):
  • tests/regressiontests/forms/tests/forms.py

    diff --git a/tests/regressiontests/forms/tests/forms.py b/tests/regressiontests/forms/tests/forms.py
    index 91db449..7f8fdcb 100644
    a b class FormsTestCase(TestCase):  
    789789        f = Person()
    790790        self.assertEqual(f['gender'].field.choices, [('f', 'Female'), ('m', 'Male')])
    791791
     792    def test_validators_independance(self):
     793        """ Test that we are able to modify a form field validators list without polluting
     794            other forms """
     795        from django.core.validators import MaxValueValidator
     796        class MyForm(Form):
     797            myfield = CharField(max_length=25)
     798
     799        f1 = MyForm()
     800        f2 = MyForm()
     801
     802        f1.fields['myfield'].validators[0] = MaxValueValidator(12)
     803        self.assertFalse(f1.fields['myfield'].validators[0] == f2.fields['myfield'].validators[0])
     804
    792805    def test_hidden_widget(self):
    793806        # HiddenInput widgets are displayed differently in the as_table(), as_ul())
    794807        # and as_p() output of a Form -- their verbose names are not displayed, and a
Back to Top