You can try writing a function that returns a function.
def date_range_validator(min_date, max_date): def innerfn(date_to_test): if not (min_date <= date_to_test <= max_date): raise ValidationError( 'Inappropriate date: %s is not between %s and %s' % (date_to_test, min_date, max_date) ) return innerfn
Then you can create the validator you need by calling this function:
class DateForm(forms.Form): forms.DateField( validators=[ date_range_validator( datetime.date(2012, 06, 01), datetime.date(2013, 03, 31) ) ] )
(thanks for fixing @ user2577922 )
PS I have not tested this, but I hope you get an idea - write a function that takes two dates that you want to have as the boundaries of your range, and that returns a function that checks that the date in which it is passed is in range.
source share