plonezopezope.interface

Specify DateTime format on zope.schema.Date on Plone


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?


Solution

  • 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.