Ticket #11303: show_hidden_initial_boolean.diff

File show_hidden_initial_boolean.diff, 1.9 KB (added by punteney, 5 years ago)

updated patch and test case

  • django/forms/widgets.py

     
    386386
    387387    def _has_changed(self, initial, data):
    388388        # Sometimes data or initial could be None or u'' which should be the
    389         # same thing as False.
     389        # same thing as False. If show_hidden_initial is true on the field then
     390        # the initial value will come through as the string 'False' which when
     391        # converted to boolean returns True
     392        if initial == 'False':
     393            initial = False
    390394        return bool(initial) != bool(data)
    391395
    392396class Select(Widget):
  • tests/regressiontests/forms/forms.py

     
    17951795>>> form.is_valid()
    17961796True
    17971797
     1798
     1799# BooleanFields with show_hidden_initial report changed when they haven't #######################
     1800
     1801>>> class SubscriberForm(Form):
     1802...     name = CharField(show_hidden_initial=True)
     1803...     new_subscriber = BooleanField(show_hidden_initial=True, required=False)
     1804...
     1805>>> def my_function(method, post_data):
     1806...     if method == 'POST':
     1807...         form = SubscriberForm(post_data)
     1808...     else:
     1809...         form = SubscriberForm()
     1810...     if form.is_valid():
     1811...         return form.changed_data
     1812...     t = Template('<form action="" method="post">\n<table>\n{{ form }}\n</table>\n<input type="submit" />\n</form>')
     1813...     return t.render(Context({'form': form}))
     1814...
     1815>>> print my_function('POST', {'name': 'James', 'initial-name': 'James', 'new_subscriber': False, 'initial-new_subscriber': 'False'})
     1816[]
     1817
    17981818# Extracting hidden and visible fields ######################################
    17991819
    18001820>>> class SongForm(Form):
Back to Top