mirror of
https://github.com/django/django.git
synced 2025-10-24 06:06:09 +00:00
Fixed #25078 -- Added support for disabled form fields
Thanks Keryn Knight and Tim Graham for the reviews.
This commit is contained in:
@@ -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:
|
||||
|
||||
@@ -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()
|
||||
|
||||
Reference in New Issue
Block a user