jazzband / django-floppyforms

Full control of form rendering in the templates.
http://django-floppyforms.readthedocs.org/
Other
839 stars 145 forks source link

bug SelectDateWidget #44

Closed fsx999 closed 11 years ago

fsx999 commented 12 years ago

Hi. Sorry for my bad english. I found a bug : The SelectDateWidget has not empty choice ( ----- choices). The class has a none_value, but it isn't use. The problem is : the fisrt load of the page, the selectDateWideget has already value selected. My solution :

    none_value = ('', '---')
    #none_value = (0, '---')
    def render(self, name, value, attrs=None, extra_context={}):
    try:
        year_val, month_val, day_val = value.year, value.month, value.day
    except AttributeError:
        year_val = month_val = day_val = None
        if isinstance(value, basestring):
            if settings.USE_L10N:
                try:
                    input_format = formats.get_format(
                        'DATE_INPUT_FORMATS'
                    )[0]
                    v = datetime.datetime.strptime(value, input_format)
                    year_val, month_val, day_val = v.year, v.month, v.day
                except ValueError:
                    pass
            else:
                match = RE_DATE.match(value)
                if match:
                    year_val, month_val, day_val = map(int, match.groups())

    context = self.get_context(name, value, attrs=attrs,
                               extra_context=extra_context)

    context['year_choices'] = [self.none_value]+[(i, i) for i in self.years]
    #context['year_choices'] = [(i, i) for i in self.years]
    context['year_val'] = year_val

    context['month_choices'] = [self.none_value]+MONTHS.items()
    #context['month_choices'] = MONTHS.items()
    context['month_val'] = month_val

    context['day_choices'] =[self.none_value] + [(i, i) for i in range(1, 32)]
    #context['day_choices'] = [(i, i) for i in range(1, 32)]
    context['day_val'] = day_val

    return loader.render_to_string(self.template_name, context)

Thx

gregmuellegger commented 12 years ago

Hm really seems that django's implementation handles this differently. What is irritating me though is that the empty choices in django's implementation are only shown if no value (e.g. no initial value is provided and not on already submitted forms) even if the form is not required.

Maybe we should ignore django's behaviour here and always show the empty choices (e.g. '----') if required==False. That makes much more sense here IMHO.