Validate a MultiValueField with a custom MultiValueWidget in Django

2k views Asked by At

I want to have a field and a widget that lets have an "approximated" date, the idea is have one and only one of the following :

  • Exact date
  • A year and maybe a month
  • An initial and an end year

Some compressed valid values could be:

  • "1988/08/07,,,,"
  • ",1988,,,"
  • ",1988,08,,"
  • ",,,1997,1999"

The problem is that I'm unable to validate the fields to have only one of the three options described above, in the Field definition I tried creating validate and clean methods, but it's not getting validating, I suspect that the link between the fields and widgets is not right. What am I missing?

To accomplish this I created a Widget:

class ApproximatedDateWidget(forms.MultiWidget):
    def __init__(self, attrs=None,choices=()):
        self.widgets = (
            forms.DateInput(attrs=attrs),
            forms.TextInput(attrs=attrs),
            forms.Select(attrs=attrs,choices=choices),
            forms.TextInput(attrs=attrs),
            forms.TextInput(attrs=attrs),
        )
        super(ApproximatedDateWidget, self).__init__(self.widgets, attrs=attrs)

    def decompress(self,value):
        try :
            print value
            if value:
                l=value.split(",")
                print l
                if l[0] and l[0]!="":                    
                    d=l[0].split("/")
                    print d
                    return [date(int(d[0]),int(d[1]),int(d[2])),None,None,None,None]
                else:
                    return [None, l[1], l[2], l[3], l[4]]
            return [None, None, None, None, None]
        except:
            raise
            return [None, None, None, None, None]

    def format_output(self,rendered_widgets):
        return mark_safe(u"""<table>
        <tr><td>Exact date:</td><td colspan="3">{0}</td></tr>
        <tr><td>Year and month:</td><td>{1}</td><td colspan="2">{2}</td></tr>
        <tr><td></td><td>Year</td><td colspan="2">Month</td></tr>
        <tr><td>Between:</td><td>{3}</td><td>and</td><td>{4}</td></tr>
        <tr><td></td><td>Year</td><td></td><td>Year<td></tr>
        </table>
        """.format(*rendered_widgets))

And the form Field:

from tools import MonthChoices
from validators import nullableinteger

class ApproximatedDateField(forms.MultiValueField):
    """
    Field that allows having an approximated date, one of
    exact date, month and year or range of years
    """


    def __init__(self, required=False, widget=None, label=None, initial=None,
                 help_text=None,attrs={}, *args, **kwargs):
        self.widget = ApproximatedDateWidget(attrs=attrs,choices=MonthChoices)
        widgets=self.widget.widgets
        fields = (
            forms.DateField(required=required,validators=[lessthantomorrow],widget=widgets[0]),
            forms.CharField(widget=widgets[1],required=required,max_length=4,validators=[nullableinteger]),
            forms.ChoiceField(required=required,choices=MonthChoices,widget=widgets[2]),
            forms.CharField(widget=widgets[3],required=required,max_length=4,validators=[nullableinteger]),
            forms.CharField(widget=widgets[4],required=required,max_length=4,validators=[nullableinteger]),
        )
        super(ApproximatedDateField, self).__init__(fields=fields,required=required, widget=widget, 
              label=label, initial=initial, help_text=help_text)

    def compress(self,data_list):
        return """{0},{1},{2},{3},{4}""".format(data_list[0].strftime("%d/%m/%Y"),data_list[1],(data_list[2] and data_list[2].id) or "",data_list[3],data_list[4])

    def validate(self,values):
        super(ApproximatedDateField, self).validate(values)
        if values[0] is not None and (values[1] is not None or values[2] is not None) and (values[1] is not None or values[2] is not None):
            raise ValidationError(u'Please only fill exact date or year and month or the range years')

    def clean(self,values):
        super(ApproximatedDateField, self).clean(values)
        if values[0] is not None and (values[1] is not None or values[2] is not None) and (values[1] is not None or values[2] is not None):
            raise ValidationError(u'Please only fill exact date or year and month or the range years')
1

There are 1 answers

1
Brandon Taylor On

I wrote a custom widget and multi-value field to handle feet and inches: http://djangosnippets.org/snippets/2327/, and also another one that is a math captcha: https://github.com/btaylordesign/django-simple-math-captcha

I put my validation in the compress method of the MultiValueField, which worked great for these. You might have a look at that snippet and repo and see if that gets you on the right track.