#27263: Don't apply the rest of validators if the first one failed
---------------------------------+--------------------------------------
     Reporter:  Alexey Rogachev  |                    Owner:  nobody
         Type:  New feature      |                   Status:  new
    Component:  Forms            |                  Version:  master
     Severity:  Normal           |               Resolution:
     Keywords:  form, validator  |             Triage Stage:  Unreviewed
    Has patch:  0                |      Needs documentation:  0
  Needs tests:  0                |  Patch needs improvement:  0
Easy pickings:  0                |                    UI/UX:  0
---------------------------------+--------------------------------------
Description changed by Alexey Rogachev:

Old description:

> I have a couple of validators for files, `FileSizeValidator` and
> `FileTypeValidator`.
>
> I attach it to form field like this:
>
> {{{#!python
> class ChartForm(forms.ModelForm):
>     import_file = forms.FileField(required=False, label='File for
> import', validators = [
>         FileSizeValidator(max_size='500 kb'),
>         FileTypeValidator(extensions=('xlsx',))
>     ])
> }}}
>
> Validators are extended from `object` and implement `__init__` and
> `__call__` methods (latter raises `ValidationError`).
>
> The problem is, when I load the bigger file and with different extension,
> both validators execute and both error messages are shown for field
> (about exceeding max limit and wrong extension).
>
> What I want - if first validator failed, don't even apply the rest of
> validators. For this specific scenario is not crucial, but in case of
> bigger amount of validators and more complex checks it can be a problem.
> For example if we need to read the file and verify that something exists
> there, why whe should do that if a size limit already exceeded.
>
> Another option can be customizing each validator to not execute in case
> of existing errors.
>
> I tried to add additional `field` argument to validator:
>
> {{{#!python
> class FileSizeValidator(object):
>     def __init__(self, max_size, field=None):
>         self.max_size = max_size
>         self.field = field
> }}}
>
> and pass it in form's `__init __` method:
>
> {{{#!python
> class ChartForm(forms.ModelForm):
>     def __init__(self, *args, **kwargs):
>         super(ChartForm, self).__init__(*args, **kwargs)
>         for validator in self.fields['import_file'].validators:
>             validator.form = self.fields['import_file']
> }}}
>
> but I don't know how to properly call `self.field.clean()` which requires
> `data`. Besides that, this is not good approach, because some other
> validators (for example from Django core) can be attached to field.
>
> Also this will require creating some additional mixin that must be
> attached along with validators.
>
> I think there is should be much simpler and elegant solution.
>
> Can't find related questions or info in docs.

New description:

 I have a couple of validators for files, `FileSizeValidator` and
 `FileTypeValidator`.

 I attach it to form field like this:

 {{{#!python
 class ChartForm(forms.ModelForm):
     import_file = forms.FileField(required=False, label='File for import',
 validators = [
         FileSizeValidator(max_size='500 kb'),
         FileTypeValidator(extensions=('xlsx',))
     ])
 }}}

 Validators are extended from `object` and implement `__init__` and
 `__call__` methods (latter raises `ValidationError`).

 The problem is, when I load the bigger file and with different extension,
 both validators execute and both error messages are shown for field (about
 exceeding max limit and wrong extension).

 What I want - if first validator failed, don't even apply the rest of
 validators. For this specific scenario is not crucial, but in case of
 bigger amount of validators and more complex checks it can be a problem.
 For example if we need to read the file and verify that something exists
 there, why whe should do that if a size limit already exceeded.

 Another option can be customizing each validator to not execute in case of
 existing errors.

 I tried to add additional `field` argument to validator:

 {{{#!python
 class FileSizeValidator(object):
     def __init__(self, max_size, field):
         self.max_size = max_size
         self.field = field
 }}}

 and pass it in form's `__init __` method:

 {{{#!python
 class ChartForm(forms.ModelForm):
     def __init__(self, *args, **kwargs):
         super(ChartForm, self).__init__(*args, **kwargs)
         for validator in self.fields['import_file'].validators:
             validator.form = self.fields['import_file']
 }}}

 but I don't know how to properly call `self.field.clean()` which requires
 `data`. Besides that, this is not good approach, because some other
 validators (for example from Django core) can be attached to field.

 Also this will require creating some additional mixin that must be
 attached along with validators.

 I think there is should be much simpler and elegant solution.

 Can't find related questions or info in docs.

--

--
Ticket URL: <https://code.djangoproject.com/ticket/27263#comment:3>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-updates+unsubscr...@googlegroups.com.
To post to this group, send email to django-updates@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/067.f61f51b0495a8460663c16c8f470f84d%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to