[Answer]-How to continue checking errors after I found one in "if" – Django, Python

1👍

How about this?

def clean_file_name(self):
    name = self.cleaned_data['file_name']
    error = ''
    if len(name) < 2:
        error += 'File name is too short'
    if FileDescription.objects.filter(file_name = name).exists():
        if error:
            error += ' ' 
        error += 'File with this name already exists'
    if error:
        raise forms.ValidationError(error)
    return name

Or did you want them as two separate bullets? If so, you’ll need to add them to the self.errors list (see Alexander’s answer) and then raise a ValidationError to make sure the form doesn’t process.

👤Tom

0👍

From my code, hope it helps.

if isinstance(self.errors.get('field_name'), forms.util.ErrorList):
    self.errors['field_name'].append(_('My error'))
else:
    self.errors['field_name'] = forms.util.ErrorList([_('MyError')])

Leave a comment