Discussion:
[FE-discuss] formencode validator DateConverter in 1.2
Michael van Tellingen
2009-01-09 10:25:10 UTC
Permalink
Hello everyone,

I always use the DateConverter for validating dates in my formencode
schemas. However i've recently tried upgrading to
formencode 1.2 but unfortunatly it doesn't support dashes as separators
anymore.

To summarize; the following did work in 1.1 but doesn't in 1.2:
validators.DateConverter(month_style='dd-mm-yyyy')

The offending code in 1.2 is:

def __init__(self, *args, **kw):
super(DateConverter, self).__init__(*args, **kw)
if not self.month_style in ('dd/mm/yyyy', 'mm/dd/yyyy'):
raise TypeError("Bad month_style: %r" % self.month_style)

Is it possible that this get's fixed in the trunk? Thanks ;-)


Regards,
Michael van Tellingen
Ian Bicking
2009-01-09 17:57:16 UTC
Permalink
Post by Michael van Tellingen
Hello everyone,
I always use the DateConverter for validating dates in my formencode
schemas. However i've recently tried upgrading to
formencode 1.2 but unfortunatly it doesn't support dashes as separators
anymore.
validators.DateConverter(month_style='dd-mm-yyyy')
It actually allows / or - as separators, these two strings are just
indications of which month style you want (before I think it'd treat
dd-mm-yyyy and mm-dd-yyyy identically).
--
Ian Bicking : ***@colorstudy.com : http://blog.ianbicking.org
Michael van Tellingen
2009-01-12 11:03:32 UTC
Permalink
Post by Ian Bicking
Post by Michael van Tellingen
Hello everyone,
I always use the DateConverter for validating dates in my formencode
schemas. However i've recently tried upgrading to
formencode 1.2 but unfortunatly it doesn't support dashes as separators
anymore.
validators.DateConverter(month_style='dd-mm-yyyy')
It actually allows / or - as separators, these two strings are just
indications of which month style you want (before I think it'd treat
dd-mm-yyyy and mm-dd-yyyy identically).
Ok, I actually just assumed that it was required to use the slash as
separator in the new version. My bad ;-)

Thanks,
Michael
Post by Ian Bicking
--
Matthew Wilson
2009-01-09 18:31:44 UTC
Permalink
On Fri, Jan 9, 2009 at 5:25 AM, Michael van Tellingen
Post by Michael van Tellingen
Hello everyone,
I always use the DateConverter for validating dates in my formencode
schemas. However i've recently tried upgrading to
formencode 1.2 but unfortunatly it doesn't support dashes as separators
anymore.
validators.DateConverter(month_style='dd-mm-yyyy')
super(DateConverter, self).__init__(*args, **kw)
raise TypeError("Bad month_style: %r" % self.month_style)
Is it possible that this get's fixed in the trunk? Thanks ;-)
I wrote a slightly different DateConverter that accepts a list of
expected formats. This eliminates the need for really complex regular
expressions.

Here's the code for it. The docstring has some example uses (and tests).

from formencode.validators import FancyValidator

class WorseDateConverter(FancyValidator):

"""
Requires a list of valid date format strings.
Post by Michael van Tellingen
wdc = WorseDateConverter(['%m-%d-%Y', '%m/%d/%Y', '%Y-%m-%d'])
wdc.to_python('09-06-2008')
datetime.date(2008, 9, 6)
Post by Michael van Tellingen
wdc.to_python('09/06/2008')
datetime.date(2008, 9, 6)
Post by Michael van Tellingen
wdc.to_python('2008-09-06')
datetime.date(2008, 9, 6)
Post by Michael van Tellingen
wdc.to_python('2008/09/06') # slashes instead of dashes.
Traceback (most recent call last):
...
Invalid: I couldn't parse '2008/09/06' with any of my formats!
"""

def __init__(self, dateformats):
self.dateformats = dateformats

def to_python(self, value, state=None):

if value is None:
return

for datefmt in self.dateformats:

try:
return datetime.strptime(value, datefmt).date()

except ValueError:
pass

else:
msg = "I couldn't parse '%s' with any of my formats!" % value

raise Invalid(msg, value, state)

def from_python(self, value, state=None):
if value is None:
return None
else:
return value.strftime(self.dateformats[0])

Maybe that helps.

Matt
--
Matthew Wilson
***@tplus1.com
http://tplus1.com
Loading...