Allow validators to short-circuit in form field validation

68 views
Skip to first unread message

Alexey Rogachev

unread,
Sep 26, 2016, 7:05:36 AM9/26/16
to Django developers (Contributions to Django itself)
I opened ticket https://code.djangoproject.com/ticket/27263.

Do you think the solution I suggested in comment is OK?

charettes

unread,
Sep 26, 2016, 8:34:48 AM9/26/16
to Django developers (Contributions to Django itself)
Hi Alexey,

I'm not sure I understand why the approach Aymeric suggested is not viable for
your use case.

It can be implemented in a few lines and doesn't require any modification to
Django core.


class ShortCircuitValidator(object):
    def __init__(self, *validators):
        self.validators = validators

    def __call__(self, value):
        for validator in self.validators:
            validator(value)


class FileForm(forms.Form):
    file = forms.FileField(
        validators=[ShortCircuitValidator(
            FileSizeValidator(max_size='500 kb'),
            FileTypeValidator(extensions=['xlsx']),
        )],
    )

Simon

Aymeric Augustin

unread,
Sep 26, 2016, 10:13:35 AM9/26/16
to django-d...@googlegroups.com
Note that I made this comment in reaction to Alexey’s email here :-)

-- 
Aymeric.

--
You received this message because you are subscribed to the Google Groups "Django developers (Contributions to Django itself)" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-develop...@googlegroups.com.
To post to this group, send email to django-d...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-developers.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-developers/5895bcc4-1289-4adf-80a3-f595806d49a7%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages