Fixed #26534 -- Fixed boolean form fields has_changed() with hidden input.

This commit is contained in:
David Sanders 2016-04-24 12:06:30 -07:00 committed by Simon Charette
parent 188883048e
commit 218175b09d
3 changed files with 15 additions and 14 deletions

View File

@ -720,12 +720,9 @@ class BooleanField(Field):
raise ValidationError(self.error_messages['required'], code='required') raise ValidationError(self.error_messages['required'], code='required')
def has_changed(self, initial, data): def has_changed(self, initial, data):
# Sometimes data or initial could be None or '' which should be the # Sometimes data or initial may be a string equivalent of a boolean
# same thing as False. # so we should run it through to_python first to get a boolean value
if initial == 'False': return self.to_python(initial) != self.to_python(data)
# show_hidden_initial may have transformed False to 'False'
initial = False
return bool(initial) != bool(data)
class NullBooleanField(BooleanField): class NullBooleanField(BooleanField):
@ -754,14 +751,6 @@ class NullBooleanField(BooleanField):
def validate(self, value): def validate(self, value):
pass pass
def has_changed(self, initial, data):
# None (unknown) and False (No) are not the same
if initial is not None:
initial = bool(initial)
if data is not None:
data = bool(data)
return initial != data
class CallableChoiceIterator(object): class CallableChoiceIterator(object):
def __init__(self, choices_func): def __init__(self, choices_func):

View File

@ -54,3 +54,8 @@ class BooleanFieldTest(SimpleTestCase):
self.assertTrue(f.has_changed(True, '')) self.assertTrue(f.has_changed(True, ''))
# Initial value may have mutated to a string due to show_hidden_initial (#19537) # Initial value may have mutated to a string due to show_hidden_initial (#19537)
self.assertTrue(f.has_changed('False', 'on')) self.assertTrue(f.has_changed('False', 'on'))
# HiddenInput widget sends string values for boolean but doesn't clean them in value_from_datadict
self.assertFalse(f.has_changed(False, 'False'))
self.assertFalse(f.has_changed(True, 'True'))
self.assertTrue(f.has_changed(False, 'True'))
self.assertTrue(f.has_changed(True, 'False'))

View File

@ -67,3 +67,10 @@ class NullBooleanFieldTest(FormFieldAssertionsMixin, SimpleTestCase):
self.assertTrue(f.has_changed(True, False)) self.assertTrue(f.has_changed(True, False))
self.assertTrue(f.has_changed(True, None)) self.assertTrue(f.has_changed(True, None))
self.assertTrue(f.has_changed(True, False)) self.assertTrue(f.has_changed(True, False))
# HiddenInput widget sends string values for boolean but doesn't clean them in value_from_datadict
self.assertFalse(f.has_changed(False, 'False'))
self.assertFalse(f.has_changed(True, 'True'))
self.assertFalse(f.has_changed(None, ''))
self.assertTrue(f.has_changed(False, 'True'))
self.assertTrue(f.has_changed(True, 'False'))
self.assertTrue(f.has_changed(None, 'False'))