Created
September 26, 2013 19:21
-
-
Save julianwachholz/6719216 to your computer and use it in GitHub Desktop.
Easy single `<input>` month/year form field for Django.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class CreditCardExpirationField(forms.DateField): | |
default_error_messages = { | |
'min_value': _("This card has expired."), | |
'invalid': _("Please specify a valid expiration date.") | |
} | |
widget = forms.DateInput(format='%m/%Y') | |
input_formats = ('%m/%Y', '%m/%y') | |
default_validators = [ | |
MinValueValidator(now().date()), | |
] | |
def to_python(self, value): | |
""" | |
Set day to last day of month. | |
Credit cards are valid through the last day of the specified month. | |
""" | |
value = super(CreditCardExpirationField, self).to_python(value) | |
last_day = monthrange(value.year, value.month)[1] | |
return datetime.date(value.year, value.month, last_day) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
There is a fields for month field?