torchbox / tbxforms

A Torchbox-flavoured template pack for django-crispy-forms, adapted from crispy-forms-gds.

Home Page:https://pypi.org/project/tbxforms/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Empty value on optional DateInputField causes error

KIRA009 opened this issue · comments

commented

Description

A form that has an optional DateInputField raises a ValueError if user submits the form leaving the date field blank.

Cause

This is caused by

return self.compress([])
which fails at

tbxforms/tbxforms/fields.py

Lines 170 to 182 in f1f5c26

def compress(self, data_list):
"""
Convert the values entered into the fields as a ``date``.
Args:
data_list (tuple): a 3-tuple the of values entered into the fields.
Returns:
the ``date`` for the values entered in the day, month and year
fields. If any of the field are blank then None is returned.
"""
day, month, year = data_list
because it expects data_list to be a 3-tuple and instead receives an empty list

Solution

The solution, unless I am missing something, is to simply check if data_list indeed contains 3 values, before unpacking them

if len(data_list) != 3:
    return None