Date widget just for some dates

I want to make a field (as a behaviour if possible).
I want to use the date time widget (or something else, like a string field with a javascript if that is better).
I want the user to be able to choose between dates from 01.01.2015 and today (current date).
A validator will do, but even better would be if the widget did not show dates before 2015 and after current date

How is this best done?

I think you can do this via Widget Manipulation like described in the Docs. Check the DateRange in Code and show/hide the widget.

import z3c.form.interfaces
...

    def updateWidgets(self):
        self.widgets["getAvailability"].mode = z3c.form.interfaces.HIDDEN_MODE

I think Pick-A-Date is configurable to this effect, assuming you are using Mockup (Plone 5) or plone.app.widgets 1.x in Plone 4. You would add configuration for the date range in the 'date' dict of the form directive for the widget.

Sean

Thanks everyone.

I could not find that mockup has options for this (?), but a (new) javascript works.

That said: I discovered that it is possible to set hardcoded dates TTW (in Dexterity Content types), like:

<schema>
    <field name="date" type="zope.schema.Date">
       <min>2015-05-12</min>

I assume it is not possible to use something like 'current date here', in case it is, please let me know...

Remember that mockup just wraps a pattern around PickADate. PickADate supports min/max being set programmatically in options. You can put this in your widget configuration:

In HTML, this would look like:

<input
    type="text"
    class="pat-pickadate"
    data-pat-pickadate='{"date":{"min":[2015,1,1]}}'
    />

And you can pass options for mockup-based widgets in your schema using form directives, IIRC, looks like this:

# imports elided

class IWhenWhere(model.Schema):
    """Some content schema"""

    directives.widget(
        'when',
        pattern_options={
            'date': {
                min: [2015, 1, 1]
                }
            }
        )
    when = schema.Date()

    where = schema.TextLine(default=u'Anywhere but here')

Sean

1 Like

I had no idea that you could use directives like that, that is great.
I did not manage to get it to work, probably because I use it inside a tuple:

Anyway, for a tuple this works (I am putting the code here since it was not obvious for me, so maybe someone else could need it one day.... ):

def theDefaultValue():
    return datetime.date.today() - datetime.timedelta(1)

def maxValue():
    return datetime.date.today()

class IDate(schema.Date):
    """ Data field for tuple. min and max are not working inside the tuple
    they need to be set in the tuple instead"""
    date=schema.Date(
            title=_(u"Dato"),
            defaultFactory=theDefaultValue,
    )
    
class IGabrielBehavior(form.Schema):
    """ Fields to construct the gabriel
    graphs from JSON URLs"""
    
    dates = schema.Tuple(
    	title=_(u"Datoer"),
    	required=True,
    	default = (theDefaultValue(),),
    	value_type=IDate(
            title=_(u"Dato"),
            min=datetime.date(2015, 5, 12),
            max=datetime.date.today(),
            defaultFactory=theDefaultValue,
        )
    )

This will also 'pre-set' one date, so one does not need to add 'the first date'

1 Like

How, exactly, are you using this / representing this on a form with a widget-per-date? If you are in fact using the mockup widgets, you ought to be able to set min/max.

Sean