From cff19f38a307dfbae3994a676fa41026b923a233 Mon Sep 17 00:00:00 2001 From: Lance Edgar Date: Mon, 19 Sep 2016 13:13:53 -0500 Subject: [PATCH] Improve validation for FormAlchemy date field renderer --- tailbone/forms/renderers/core.py | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/tailbone/forms/renderers/core.py b/tailbone/forms/renderers/core.py index e68d4777..0f3c925c 100644 --- a/tailbone/forms/renderers/core.py +++ b/tailbone/forms/renderers/core.py @@ -2,7 +2,7 @@ ################################################################################ # # Rattail -- Retail Software Framework -# Copyright © 2010-2015 Lance Edgar +# Copyright © 2010-2016 Lance Edgar # # This file is part of Rattail. # @@ -24,16 +24,16 @@ Core Field Renderers """ -from __future__ import unicode_literals +from __future__ import unicode_literals, absolute_import import datetime -import formalchemy +import formalchemy as fa from formalchemy.fields import AbstractField from pyramid.renderers import render -class CustomFieldRenderer(formalchemy.FieldRenderer): +class CustomFieldRenderer(fa.FieldRenderer): """ 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 @@ -59,7 +59,7 @@ class CustomFieldRenderer(formalchemy.FieldRenderer): 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 in edit mode. @@ -79,7 +79,12 @@ class DateFieldRenderer(CustomFieldRenderer, formalchemy.DateFieldRenderer): value = self._serialized_value() if not value: 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): return self.params.getone(self.name)