Friday, June 30, 2017

Re: SelectDateWidget when DateField is not required

I can't reproduce a crash. If I select only a month, I see a validation error, "Enter a valid date."

On Monday, June 12, 2017 at 6:28:29 AM UTC-4, Nonverbis M wrote:

Django 1.11.2

I'd like to use SelectDateWidget.


class FrameDate(models.Model):
    through_date
= models.DateField(null=True,
                                    blank
=True,
                                    verbose_name
=_("through"))
   
...

class FrameDateForm(ModelForm):

   
def clean_through_date(self):
       
pass # Breakpoint

   
class Meta:
            model
= FrameDate
            exclude
= []

            years
= range(1800, datetime.datetime.now().year + 1)

            widgets
= {
               
'frame': forms.HiddenInput(),
               
'from_date': forms.SelectDateWidget(years=years),
               
'through_date': forms.SelectDateWidget(years=years)
           
}



As we can see, this DateField is not required. And have an empty choice at the top of the list ("---").

The problem is that when a user inputs only a month we'll get ValueError springing out to the user in case of Debug=True.

There is that method clean_through_date and the break point in it. But in case of incomplete set of month, day and year the program will not even call this clean_through_date() method. This method is called in two cases:

1) if the through_date is completely empty.

2) if it is completely filled.

If it is partially filled, form validation is ignored completely. The program explodes at CreateView.

Could you comment on it? And give me a kick here: how to warn the user that s/he must either fill month, day and year or don't touch the through_date at all.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/a1744ff0-11d9-4e04-ad18-393765e4bfec%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment