Code

Opened 4 years ago

Closed 4 years ago

Last modified 3 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: UI/UX:

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

Download all attachments as: .zip

Change History (24)

comment:1 Changed 4 years ago by jezdez

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset

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 4 years ago by bryanveloso

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 4 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 4 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 4 years ago by russellm

  • milestone set to 1.2
  • Triage Stage changed from Unreviewed to Accepted

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 4 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 4 years ago by anonymous

  • Owner changed from nobody to anonymous
  • Status changed from new to assigned

comment:8 Changed 4 years ago by walteralini

  • Owner changed from anonymous to walteralini
  • Status changed from assigned to new

comment:9 Changed 4 years ago by walteralini

  • Status changed from new to assigned

Changed 4 years ago by walteralini

comment:10 Changed 4 years ago by walteralini

  • Has patch set
  • Needs tests set

comment:11 Changed 4 years ago by walteralini

  • Owner changed from walteralini to nobody
  • Status changed from assigned to new

Changed 4 years ago by bmihelac

comment:12 Changed 4 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 4 years ago by bmihelac

comment:13 Changed 4 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 4 years ago by bmihelac

comment:14 Changed 4 years ago by anonymous

  • Owner changed from nobody to anonymous
  • Status changed from new to assigned

comment:15 Changed 4 years ago by bmihelac

  • Owner changed from anonymous to bmihelac
  • Status changed from assigned to new

comment:16 Changed 4 years ago by anonymous

  • Resolution set to fixed
  • Status changed from new to closed

comment:17 Changed 4 years ago by jezdez

  • Resolution fixed deleted
  • Status changed from closed to reopened

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

comment:18 Changed 4 years ago by russellm

  • Resolution set to fixed
  • Status changed from reopened to closed

(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 4 years ago by russellm

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 3 years ago by jacob

  • milestone 1.2 deleted

Milestone 1.2 deleted

Add Comment

Modify Ticket

Change Properties
<Author field>
Action
as closed
as The resolution will be set. Next status will be 'closed'
The resolution will be deleted. Next status will be 'new'
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.