diff options
| author | Claude Paroz <claude@2xlibre.net> | 2015-07-07 21:06:57 +0200 |
|---|---|---|
| committer | Claude Paroz <claude@2xlibre.net> | 2015-07-16 19:36:56 +0200 |
| commit | 1ef4aeab403f7fb74c0b9b11fde854fd03afc40c (patch) | |
| tree | b3852ad0aefffcf4fd29aacc19e1ca9529d9cdb9 /tests/forms_tests | |
| parent | 1fed8dd715cc734f51c162d4362d34d357925bc4 (diff) | |
Fixed #25078 -- Added support for disabled form fields
Thanks Keryn Knight and Tim Graham for the reviews.
Diffstat (limited to 'tests/forms_tests')
| -rw-r--r-- | tests/forms_tests/tests/test_fields.py | 10 | ||||
| -rw-r--r-- | tests/forms_tests/tests/test_forms.py | 31 |
2 files changed, 41 insertions, 0 deletions
diff --git a/tests/forms_tests/tests/test_fields.py b/tests/forms_tests/tests/test_fields.py index 9c341ca7a3..1644bda4e2 100644 --- a/tests/forms_tests/tests/test_fields.py +++ b/tests/forms_tests/tests/test_fields.py @@ -176,6 +176,10 @@ class FieldsTests(SimpleTestCase): self.assertEqual(f.clean(' 1'), ' 1') self.assertEqual(f.clean('1 '), '1 ') + def test_charfield_disabled(self): + f = CharField(disabled=True) + self.assertWidgetRendersTo(f, '<input type="text" name="f" id="id_f" disabled />') + # IntegerField ################################################################ def test_integerfield_1(self): @@ -1076,6 +1080,12 @@ class FieldsTests(SimpleTestCase): form = ChoiceFieldForm() self.assertEqual([('P', 'Paul')], list(form.fields['choicefield'].choices)) + def test_choicefield_disabled(self): + f = ChoiceField(choices=[('J', 'John'), ('P', 'Paul')], disabled=True) + self.assertWidgetRendersTo(f, + '<select id="id_f" name="f" disabled><option value="J">John</option>' + '<option value="P">Paul</option></select>') + # TypedChoiceField ############################################################ # TypedChoiceField is just like ChoiceField, except that coerced types will # be returned: diff --git a/tests/forms_tests/tests/test_forms.py b/tests/forms_tests/tests/test_forms.py index 07de828334..acd1b6840a 100644 --- a/tests/forms_tests/tests/test_forms.py +++ b/tests/forms_tests/tests/test_forms.py @@ -501,6 +501,37 @@ class FormsTestCase(SimpleTestCase): <option value="P" selected="selected">Paul McCartney</option> </select>""") + def test_form_with_disabled_fields(self): + class PersonForm(Form): + name = CharField() + birthday = DateField(disabled=True) + + class PersonFormFieldInitial(Form): + name = CharField() + birthday = DateField(disabled=True, initial=datetime.date(1974, 8, 16)) + + # Disabled fields are generally not transmitted by user agents. + # The value from the form's initial data is used. + f1 = PersonForm({'name': 'John Doe'}, initial={'birthday': datetime.date(1974, 8, 16)}) + f2 = PersonFormFieldInitial({'name': 'John Doe'}) + for form in (f1, f2): + self.assertTrue(form.is_valid()) + self.assertEqual( + form.cleaned_data, + {'birthday': datetime.date(1974, 8, 16), 'name': 'John Doe'} + ) + + # Values provided in the form's data are ignored. + data = {'name': 'John Doe', 'birthday': '1984-11-10'} + f1 = PersonForm(data, initial={'birthday': datetime.date(1974, 8, 16)}) + f2 = PersonFormFieldInitial(data) + for form in (f1, f2): + self.assertTrue(form.is_valid()) + self.assertEqual( + form.cleaned_data, + {'birthday': datetime.date(1974, 8, 16), 'name': 'John Doe'} + ) + def test_hidden_data(self): class SongForm(Form): name = CharField() |
