Ticket #5959: boolean_hidden.diff

File boolean_hidden.diff, 2.2 KB (added by Chris Beaven, 16 years ago)
  • django/newforms/fields.py

     
    538538    def clean(self, value):
    539539        "Returns a Python boolean object."
    540540        super(BooleanField, self).clean(value)
    541         # Explicitly check for the string '0', which is what as hidden field
     541        # Explicitly check for the string 'False', which is what as hidden field
    542542        # will submit for False.
    543         if value == '0':
     543        if value == 'False':
    544544            return False
    545545        return bool(value)
    546546
  • tests/regressiontests/forms/fields.py

     
    914914>>> f.clean('Django rocks')
    915915True
    916916
     917A form's BooleanField with a hidden widget will output the string 'False', so
     918that should clean to the boolean value False:
     919>>> f.clean('False')
     920False
     921
    917922>>> f = BooleanField(required=False)
    918923>>> f.clean('')
    919924False
     
    930935>>> f.clean('Django rocks')
    931936True
    932937
     938A form's BooleanField with a hidden widget will output the string 'False', so
     939that should clean to the boolean value False:
     940>>> f.clean('False')
     941False
     942
    933943# ChoiceField #################################################################
    934944
    935945>>> f = ChoiceField(choices=[('1', '1'), ('2', '2')])
  • tests/regressiontests/forms/widgets.py

     
    128128>>> w.render('email', '', attrs={'class': 'special'})
    129129u'<input type="hidden" class="special" name="email" />'
    130130
     131Still renders the boolean value False as the string 'False', it's the job of the
     132field to clean that back to a boolean False if required.
     133>>> w = HiddenInput()
     134>>> w.render('get_spam', False)
     135u'<input type="hidden" name="get_spam" value="False" />'
     136
    131137# MultipleHiddenInput Widget ##################################################
    132138
    133139>>> w = MultipleHiddenInput()
Back to Top