Opened 15 years ago

Closed 15 years ago

Last modified 13 years ago

#12986 closed (fixed)

SelectDateField doesn't repost data when USE_L10N = True and LANGUAGE_CODE = 'nl'

Reported by: wim@… Owned by: bmihelac
Component: Forms Version: 1.2-beta
Severity: Keywords:
Cc: bmihelac Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: yes Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

Hello,

If validation of a form fails, SelectDateField doesn't repost the data correctly, when USE_L10N = True and LANGUAGE_CODE = 'nl'.

For example, if I entered 2 februari 2010, the form redisplays as 1 januari 2010

Attachments (4)

12986.diff (1.2 KB ) - added by walteralini 15 years ago.
12986-2.diff (5.0 KB ) - added by bmihelac 15 years ago.
12986-2.2.diff (5.0 KB ) - added by bmihelac 15 years ago.
12986-3.diff (5.1 KB ) - added by bmihelac 15 years ago.

Download all attachments as: .zip

Change History (24)

comment:1 by Jannis Leidel, 15 years ago

Excuse the confusion, but do you mean the SelectDateWidget in django.forms.extras.widgets? Would you mind providing a test case or a simple example what fails for you?

comment:2 by Bryan Veloso, 15 years ago

This is actually not restricted to the given settings (USE_L10N = True and LANGUAGE_CODE = 'n1') as stated above. To clarify, if a form doesn't validate, the widget doesn't hold the value that was submitted resetting the month and date to January 1, with the year being the current year. I only noticed the latter behavior since I've been using said widget since early-2009.

comment:3 by wim@…, 15 years ago

#-- forms.py

from django.forms.extras import SelectDateWidget

class ConceptTripForm(forms.Form):

required = forms.CharField()
date = forms.DateField(label="Datum", required=True, widget=SelectDateWidget())

#-- views.py
from forms import ConcepTripForm

@render_to('trips/formtest.html')
def form_test(request):

if request.POST:

form = ConceptTripForm(request.POST)
if form.is_valid():

print "Wow!"
return HttpResponseRedirect('/')

else:

form = ConceptTripForm()

return {'form': form,}

#-- formtest.html
<form action="." method="POST">
<table>{% csrf_token %}

{{ form }}
<input type="submit" name="OK" value="OK">

</table>
</form>

When I enter:
Required: (nothing)
Datum: 2 februari 2011

What I get is a form error, obviously. But unfortunately, the date has been reset. So after the error the form shows:

  • Dit veld is verplicht. (Translation: This field is required.)

Required: (nothing)
Datum: 1 januari 2010

So the date I entered is gone.

I hope this description helps. Thanks!

comment:4 by anonymous, 15 years ago

Wow, that did look ugly, didn't it? Let's retry:

#-- forms.py

from django.forms.extras import SelectDateWidget

class ConceptTripForm(forms.Form):
    required = forms.CharField()
    date = forms.DateField(label="Datum", required=True, widget=SelectDateWidget())

#-- views.py
from forms import ConcepTripForm

@render_to('trips/formtest.html')
def form_test(request):
    if request.POST:
        form = ConceptTripForm(request.POST)
        if form.is_valid():
            print "Wow!"
            return HttpResponseRedirect('/')
    else:
        form = ConceptTripForm()
    return {'form': form,}

#-- formtest.html
<form action="." method="POST">
<table>{% csrf_token %}
    {{ form }}
    <input type="submit" name="OK" value="OK">
</table>
</form>

When I enter:
Required: (nothing)
Datum: 2 februari 2011

What I get is a form error, obviously. But unfortunately, the date has been reset. So after the error the form shows:

  • Dit veld is verplicht. (Translation: This field is required.)

Required: (nothing)
Datum: 1 januari 2010

So the date I entered is gone.

I hope this description helps. Thanks!

comment:5 by Russell Keith-Magee, 15 years ago

milestone: 1.2
Triage Stage: UnreviewedAccepted

Problem confirmed with the same code provided. I can only see it if USE_L10N is enabled and LANGUAGE_CODE='nl'. If I disable L10N or use a different language code, I don't see the problem.

comment:6 by walteralini, 15 years ago

I see the same problem when LANGUAGE_CODE is "fr" or "es". It's working for me for "en-us"

comment:7 by anonymous, 15 years ago

Owner: changed from nobody to anonymous
Status: newassigned

comment:8 by walteralini, 15 years ago

Owner: changed from anonymous to walteralini
Status: assignednew

comment:9 by walteralini, 15 years ago

Status: newassigned

by walteralini, 15 years ago

Attachment: 12986.diff added

comment:10 by walteralini, 15 years ago

Has patch: set
Needs tests: set

comment:11 by walteralini, 15 years ago

Owner: changed from walteralini to nobody
Status: assignednew

by bmihelac, 15 years ago

Attachment: 12986-2.diff added

comment:12 by bmihelac, 15 years ago

Cc: bmihelac added

Attached is patch with tests.
Code is slightly changed from original so it would also properly redisplay invalid dates when USE_L10N = True given that default input format consist of %Y, %m and %d format directives.

by bmihelac, 15 years ago

Attachment: 12986-2.2.diff added

comment:13 by bmihelac, 15 years ago

Tests moved from widgets.py to to proper extra.py.
Changed to always display blank choices for selects, so partial selection can be made and redisplayed.

by bmihelac, 15 years ago

Attachment: 12986-3.diff added

comment:14 by anonymous, 15 years ago

Owner: changed from nobody to anonymous
Status: newassigned

comment:15 by bmihelac, 15 years ago

Owner: changed from anonymous to bmihelac
Status: assignednew

comment:16 by anonymous, 15 years ago

Resolution: fixed
Status: newclosed

comment:17 by Jannis Leidel, 15 years ago

Resolution: fixed
Status: closedreopened

Please don't close tickets that haven't been fixed in trunk.

comment:18 by Russell Keith-Magee, 15 years ago

Resolution: fixed
Status: reopenedclosed

(In [13041]) Fixed #12986 -- Ensured that SelectDateField repopulates correctly when USE_L10N=True and locale has a default date input format other than %Y-%m-%d. Thanks to wim@… for the report, and walteralini for his draft patch.

comment:19 by Russell Keith-Magee, 15 years ago

This ticket has been closed because the critical bug (getting the date wrong on resubmit) has been fixed. However, the fix isn't really complete; #13427 documents an edge case where the current implementation is lacking.

comment:20 by Jacob, 13 years ago

milestone: 1.2

Milestone 1.2 deleted

Note: See TracTickets for help on using tickets.
Back to Top