I have this code that check errors in the form. After user make some mistake it displays first found error. But if mistake belongs to more than one error it will display only first one.
#forms.py
def clean_file_name(self):
name = self.cleaned_data['file_name']
if len(name) < 2:
raise forms.ValidationError('File name is too short')
if FileDescription.objects.filter(file_name = name).exists():
raise forms.ValidationError('File with this name already exists')
else:
return name
#template
<ul>
{% for field in form %}
{% for error in field.errors %}
<li>{{ error }}</li>
{% endfor %}
{% endfor %}
</ul>
if user will enter “a” and file in database with this name already exists (lets say it was there before I wrote length checking)
The output would be :
“File name is too short”
But I want both errors to be displayed
How about this?
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.