Opened 12 years ago

Last modified 12 years ago

#19537 closed Bug

Widget CheckboxInput show_hidden_initial _has_changed bug — at Version 4

Reported by: dibrovsd@… Owned by: nobody
Component: Forms Version: 1.4
Severity: Normal Keywords: show_hidden_initial
Cc: Triage Stage: Ready for checkin
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description (last modified by Claude Paroz)

django.forms.CheckboxInput have method

def _has_changed(self, initial, data):
    # Sometimes data or initial could be None or u'' which should be the
    # same thing as False.
    return bool(initial) != bool(data)

initial may be u'False' or u'True'
bool(initial) always return True for unicode len(initial) > 1

if initial == u'False' and data == u'on' always return False
bool(u'False') = True
bool(u'on') = True
path:

def _has_changed(self, initial, data):
    initial = True if initial == u'True' else False
    return initial != bool(data)

Change History (5)

comment:1 by Claude Paroz, 12 years ago

Resolution: invalid
Status: newclosed

By default, any non-empty string is considered as a True value, even "False". You should fix the initial value passed to the form in your code.

comment:2 by dibrovsd@…, 12 years ago

my initial data is bool type

ok full test case is

# View is

def document(request, project_id, document_id):
	u''' Карточка документа '''

	class TestForm(forms.Form):
		bool_field = forms.BooleanField(show_hidden_initial=True, required=False)
		
	if request.method == 'POST':
		f = TestForm(data=request.POST)
		if f.is_valid():
			print f.changed_data
	else:
		# initial is bool type not unicode!
		f = TestForm(initial={u'bool_field':False})
	
	return render(request, 'docflow/document.html', {'form':f})

# Template is

<html>
<body>
<form method='post'>
	{% csrf_token %}
	{{form.as_p}}
	<input type='submit'>					
</form>
</body>
</html>		

# action1

  • load page
  • checkbox is not cheched by default
  • check checkbox and send form (change!)
  • print to console is "[]" CheckboxInput._has_changed arg "initial" == u'False' (not bool False) bool(u'False') == True of course,
    but arg "data" == u'on' bool(u'on') == True too
    True != True => False

# action2

  • load page
  • checkbox is not cheched by default
  • send form
  • print to console is "bool_field" CheckboxInput._has_changed arg "initial" == u'False' too bool(u'False') => True,
    but arg "data" == False bool(False) == False
    widget _has_changed => true
Last edited 12 years ago by Claude Paroz (previous) (diff)

comment:3 by dibrovsd@…, 12 years ago

Component: UncategorizedForms
Resolution: invalid
Status: closednew

reopen tiket

comment:4 by Claude Paroz, 12 years ago

Description: modified (diff)

by Claude Paroz, 12 years ago

Attachment: 19537-1.diff added
Note: See TracTickets for help on using tickets.
Back to Top