Improve validation for FormAlchemy date field renderer
This commit is contained in:
parent
65661a03f5
commit
cff19f38a3
|
@ -2,7 +2,7 @@
|
||||||
################################################################################
|
################################################################################
|
||||||
#
|
#
|
||||||
# Rattail -- Retail Software Framework
|
# Rattail -- Retail Software Framework
|
||||||
# Copyright © 2010-2015 Lance Edgar
|
# Copyright © 2010-2016 Lance Edgar
|
||||||
#
|
#
|
||||||
# This file is part of Rattail.
|
# This file is part of Rattail.
|
||||||
#
|
#
|
||||||
|
@ -24,16 +24,16 @@
|
||||||
Core Field Renderers
|
Core Field Renderers
|
||||||
"""
|
"""
|
||||||
|
|
||||||
from __future__ import unicode_literals
|
from __future__ import unicode_literals, absolute_import
|
||||||
|
|
||||||
import datetime
|
import datetime
|
||||||
|
|
||||||
import formalchemy
|
import formalchemy as fa
|
||||||
from formalchemy.fields import AbstractField
|
from formalchemy.fields import AbstractField
|
||||||
from pyramid.renderers import render
|
from pyramid.renderers import render
|
||||||
|
|
||||||
|
|
||||||
class CustomFieldRenderer(formalchemy.FieldRenderer):
|
class CustomFieldRenderer(fa.FieldRenderer):
|
||||||
"""
|
"""
|
||||||
Base class for renderers which accept customization args, and "fake out"
|
Base class for renderers which accept customization args, and "fake out"
|
||||||
FormAlchemy by pretending to still be a renderer factory when in fact it's
|
FormAlchemy by pretending to still be a renderer factory when in fact it's
|
||||||
|
@ -59,7 +59,7 @@ class CustomFieldRenderer(formalchemy.FieldRenderer):
|
||||||
return self.request.rattail_config
|
return self.request.rattail_config
|
||||||
|
|
||||||
|
|
||||||
class DateFieldRenderer(CustomFieldRenderer, formalchemy.DateFieldRenderer):
|
class DateFieldRenderer(CustomFieldRenderer, fa.DateFieldRenderer):
|
||||||
"""
|
"""
|
||||||
Date field renderer which uses jQuery UI datepicker widget when rendering
|
Date field renderer which uses jQuery UI datepicker widget when rendering
|
||||||
in edit mode.
|
in edit mode.
|
||||||
|
@ -79,7 +79,12 @@ class DateFieldRenderer(CustomFieldRenderer, formalchemy.DateFieldRenderer):
|
||||||
value = self._serialized_value()
|
value = self._serialized_value()
|
||||||
if not value:
|
if not value:
|
||||||
return None
|
return None
|
||||||
return datetime.datetime.strptime(value, '%Y-%m-%d')
|
try:
|
||||||
|
return datetime.datetime.strptime(value, '%Y-%m-%d')
|
||||||
|
except ValueError:
|
||||||
|
raise fa.ValidationError("Date value must be in YYYY-MM-DD format")
|
||||||
|
except Exception as error:
|
||||||
|
raise fa.ValidationError(unicode(error))
|
||||||
|
|
||||||
def _serialized_value(self):
|
def _serialized_value(self):
|
||||||
return self.params.getone(self.name)
|
return self.params.getone(self.name)
|
||||||
|
|
Loading…
Reference in a new issue