Django

Code

Changeset 8661

Show
Ignore:
Timestamp:
08/28/08 10:06:18 (3 months ago)
Author:
jacob
Message:

Fixed #7753: clean NullBooleanField correctly when using HiddenInput. Thanks to julien and ElliottM.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • django/trunk/django/forms/fields.py

    r8616 r8661  
    595595 
    596596    def clean(self, value): 
    597         return {True: True, False: False}.get(value, None) 
     597        """ 
     598        Explicitly checks for the string 'True' and 'False', which is what a 
     599        hidden field will submit for True and False. Unlike the 
     600        Booleanfield we also need to check for True, because we are not using 
     601        the bool() function 
     602        """ 
     603        if value in (True, 'True'): 
     604            return True 
     605        elif value in (False, 'False'): 
     606            return False 
     607        else: 
     608            return None 
    598609 
    599610class ChoiceField(Field): 
  • django/trunk/tests/regressiontests/forms/fields.py

    r8391 r8661  
    10921092>>> f.clean('hello') 
    10931093 
     1094# Make sure that the internal value is preserved if using HiddenInput (#7753) 
     1095>>> class HiddenNullBooleanForm(Form): 
     1096...     hidden_nullbool1 = NullBooleanField(widget=HiddenInput, initial=True) 
     1097...     hidden_nullbool2 = NullBooleanField(widget=HiddenInput, initial=False) 
     1098>>> f = HiddenNullBooleanForm() 
     1099>>> print f 
     1100<input type="hidden" name="hidden_nullbool1" value="True" id="id_hidden_nullbool1" /><input type="hidden" name="hidden_nullbool2" value="False" id="id_hidden_nullbool2" /> 
     1101>>> f = HiddenNullBooleanForm({ 'hidden_nullbool1': 'True', 'hidden_nullbool2': 'False' }) 
     1102>>> f.full_clean() 
     1103>>> f.cleaned_data['hidden_nullbool1'] 
     1104True 
     1105>>> f.cleaned_data['hidden_nullbool2'] 
     1106False 
     1107 
    10941108# MultipleChoiceField ######################################################### 
    10951109