Opened 10 years ago

Closed 9 years ago

Last modified 8 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 10 years ago.
12986-2.diff (5.0 KB) - added by bmihelac 10 years ago.
12986-2.2.diff (5.0 KB) - added by bmihelac 10 years ago.
12986-3.diff (5.1 KB) - added by bmihelac 10 years ago.

Download all attachments as: .zip

Change History (24)

comment:1 Changed 10 years ago by Jannis Leidel

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 Changed 10 years ago by Bryan Veloso

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 Changed 10 years ago by wim@…

#-- 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 Changed 10 years ago by anonymous

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 Changed 10 years ago by Russell Keith-Magee

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 Changed 10 years ago by walteralini

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

comment:7 Changed 10 years ago by anonymous

Owner: changed from nobody to anonymous
Status: newassigned

comment:8 Changed 10 years ago by walteralini

Owner: changed from anonymous to walteralini
Status: assignednew

comment:9 Changed 10 years ago by walteralini

Status: newassigned

Changed 10 years ago by walteralini

Attachment: 12986.diff added

comment:10 Changed 10 years ago by walteralini

Has patch: set
Needs tests: set

comment:11 Changed 10 years ago by walteralini

Owner: changed from walteralini to nobody
Status: assignednew

Changed 10 years ago by bmihelac

Attachment: 12986-2.diff added

comment:12 Changed 10 years ago by bmihelac

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.

Changed 10 years ago by bmihelac

Attachment: 12986-2.2.diff added

comment:13 Changed 10 years ago by bmihelac

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.

Changed 10 years ago by bmihelac

Attachment: 12986-3.diff added

comment:14 Changed 10 years ago by anonymous

Owner: changed from nobody to anonymous
Status: newassigned

comment:15 Changed 10 years ago by bmihelac

Owner: changed from anonymous to bmihelac
Status: assignednew

comment:16 Changed 10 years ago by anonymous

Resolution: fixed
Status: newclosed

comment:17 Changed 10 years ago by Jannis Leidel

Resolution: fixed
Status: closedreopened

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

comment:18 Changed 9 years ago by Russell Keith-Magee

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 Changed 9 years ago by Russell Keith-Magee

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 Changed 8 years ago by Jacob

milestone: 1.2

Milestone 1.2 deleted

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