From 675db84f2303650140f867b31dbac9b7edf71eb3 Mon Sep 17 00:00:00 2001 From: Malcolm Tredinnick Date: Thu, 20 Mar 2008 07:44:46 +0000 Subject: [PATCH] Fixed #5917 -- More error robustness in date parsing in SelectDateWidget, plus keep the original date selected on redisplay, even if it was bogus (e.g. 31 Feb). Patch from SmileyChris. git-svn-id: http://code.djangoproject.com/svn/django/trunk@7337 bcc190cf-cafb-0310-a4f2-bffc1f526a37 --- django/newforms/extras/widgets.py | 10 ++++- tests/regressiontests/forms/extra.py | 67 ++++++++++++++++++++++++++++ 2 files changed, 75 insertions(+), 2 deletions(-) diff --git a/django/newforms/extras/widgets.py b/django/newforms/extras/widgets.py index af6b14b08d..e3ef1d7f69 100644 --- a/django/newforms/extras/widgets.py +++ b/django/newforms/extras/widgets.py @@ -3,6 +3,7 @@ Extra HTML Widget classes """ import datetime +import re from django.newforms.widgets import Widget, Select from django.utils.dates import MONTHS @@ -10,6 +11,8 @@ from django.utils.safestring import mark_safe __all__ = ('SelectDateWidget',) +RE_DATE = re.compile(r'(\d{4})-(\d\d?)-(\d\d?)$') + class SelectDateWidget(Widget): """ A Widget that splits date input into three +Accepts a datetime or a string: + +>>> w.render('mydate', datetime.date(2010, 4, 15)) == w.render('mydate', '2010-04-15') +True + +Invalid dates still render the failed date: +>>> print w.render('mydate', '2010-02-31') + + + + Using a SelectDateWidget in a form: >>> class GetDate(Form):