Prometheus
Prometheus

Reputation: 33655

Django form split into two drop down (selects)

Within my form I have expiry_date....

expiry_date = forms.DateField()

The expiry date MUST be in MMYY format i.e. 1213 for December 2013. No / or characters should be included.

But this looks to the end user a bit naf in the form. So, is it possible in Django to add in two select box 1 for month, 1 for year, then somehow put them back together to make expiry_date again before post?

i.e. I want my user to see a drop down for both month and year and select that instead.

This is what I have so far....

CARD_CHOICES = [
    ('VISA', 'VISA'),
    ('MC', 'MasterCard'),
    ('DELTA', 'DELTA'),
    ('MAESTRO', 'Maestro'),
    ('UKE', 'Visa Electron'),
    ]

DATE_INPUT_FORMATS = ('MMYY',)
class OrderForm(forms.Form):
    """
    Order Form used to collect data ready for processing.
    """
    amount = forms.FloatField()
    expiry_date = forms.DateField(input_formats=DATE_INPUT_FORMATS)
    CSV = forms.CharField(max_length=3)
    card_type = forms.TypedChoiceField(choices=CARD_CHOICES, initial='VISA')

Upvotes: 0

Views: 752

Answers (1)

karthikr
karthikr

Reputation: 99660

You basically need a MultiValueField

Here is an example implementation from: django-creditcard app

class ExpiryDateField(forms.MultiValueField):
    """
    Form field that validates credit card expiry dates.
    """

    default_error_messages = {
        'invalid_month': _(u'Please enter a valid month.'),
        'invalid_year': _(u'Please enter a valid year.'),
        'date_passed': _(u'This expiry date has passed.'),
    }

    def __init__(self, *args, **kwargs):
        today = date.today()
        error_messages = self.default_error_messages.copy()
        if 'error_messages' in kwargs:
            error_messages.update(kwargs['error_messages'])
        if 'initial' not in kwargs:
            # Set default expiry date based on current month and year
            kwargs['initial'] = today
        months = [(x, '%02d (%s)' % (x, date(2000, x, 1).strftime(MONTH_FORMAT))) for x in xrange(1, 13)]
        years = [(x, x) for x in xrange(today.year, today.year + 15)]
        fields = (
            forms.ChoiceField(choices=months, error_messages={'invalid': error_messages['invalid_month']}),
            forms.ChoiceField(choices=years, error_messages={'invalid': error_messages['invalid_year']}),
        )
        super(ExpiryDateField, self).__init__(fields, *args, **kwargs)
        self.widget = ExpiryDateWidget(widgets=[fields[0].widget, fields[1].widget])

    def clean(self, value):
        expiry_date = super(ExpiryDateField, self).clean(value)
        if date.today() > expiry_date:
            raise forms.ValidationError(self.error_messages['date_passed'])
        return expiry_date

    def compress(self, data_list):
        if data_list:
            try:
                month = int(data_list[0])
            except (ValueError, TypeError):
                raise forms.ValidationError(self.error_messages['invalid_month'])
            try:
                year = int(data_list[1])
            except (ValueError, TypeError):
                raise forms.ValidationError(self.error_messages['invalid_year'])
            try:
                day = monthrange(year, month)[1] # last day of the month
            except IllegalMonthError:
                raise forms.ValidationError(self.error_messages['invalid_month'])
            except ValueError:
                raise forms.ValidationError(self.error_messages['invalid_year'])
            return date(year, month, day)
        return None

Upvotes: 2

Related Questions