Specify DateTime format on zope.schema.Date on Plone

1.7k Views Asked by At

I'm working on a form with Formlib that looks like this:

from zope.schema import Choice, Float, Int, Date, TextLine
from Products.Five.formlib.formbase import PageForm

class ISimuladorForm(Interface):
    """
    Zope Interface for the financial simulator for sofomanec.
    """
    start_date      = Date(title=_(u'Start Date'),
                            description=_(u'Loan start date.'),
                            required=False)
.
.
.

class SimuladorForm(PageForm):
    form_fields = form.FormFields(ISimuladorForm)

The default input format for start_date is "mm/dd/yy", but users need to input the start_date in this format: "dd/mm/yy".

How do I change the default Date format for this Interface/Schema/Form?

3

There are 3 best solutions below

1
On BEST ANSWER

You can use the DateI18nWidget instead of the default DateWidget.

It takes a displayStyle attribute that controls the formatting of the value, and it'll use the request locale to format the date. displayStyle must be one of 'full', 'long', 'medium', 'short', or None and refers to the date formats defined in zope.i18n; the default is None, which I think means 'short' but this is unclear from the code.

The exact formatting is taken from the request locale, which in turn is based on the language set for the Plone site by the portal_languages tool. Thus setting the language of the site also determines what date formats the DateI18nWidget will use; these are defined in the zope.i18n package in the locales/data directory, in a set of XML files (look for the <dateFormats> element).

If this isn't satisfactory then you'll have to create a custom browser widget. Your best bet is to subclass the DateWidget yourself and provide a new _toFormValue method to format the dates the way you want.

1
On

This might be helpful to add a custom date widget to your formlib form:

http://plone.org/documentation/manual/developer-manual/forms/using-zope.formlib/customizing-the-template-and-the-widgets

I suggest to write your own date widget by deriving from one of the existing date widget classes:

http://svn.zope.org/zope.formlib/trunk/src/zope/formlib/textwidgets.py?rev=113031&view=markup

A custom conversion of the date format using the

_toFieldValue()
_fromFieldValue() 

hooks is pretty easy...look at the existing code.

0
On

This is what I did:

from zope.app.form.browser import DateI18nWidget
from zope.i18n.format import DateTimeParseError
from zope.app.form.interfaces import ConversionError


class MyDateI18nWidget(DateI18nWidget):
    displayStyle = None

    def _toFieldValue(self, input):
        if input == self._missing:
            return self.context.missing_value
        else:
            try:
                formatter = self.request.locale.dates.getFormatter(
                    self._category, (self.displayStyle or None))
                return formatter.parse(input.lower())
            except (DateTimeParseError, ValueError), v:
                raise ConversionError(_("Invalid datetime data"),
                    "%s (%r)" % (v, input))

class SimuladorForm(PageForm):
...
    form_fields['start_date'].custom_widget = MyDateI18nWidget