2

I'm using custom validation error in Django 1.6 and it's working great; however, I can only display one error at a time. How do I go about displaying all errors if the condition in the "if" statements failed?

forms.py

class BaseNameFormSet (BaseFormSet):
...
...
...
if (firstname in firstnames) or (lastname in lastnames):
    raise forms.ValidationError ('First or last name must be unique')
if (firstname == '') or (lastname == ''):
    raise forms.ValidationError ('Both first and last name must be filled out')

addname.html

...
...
...
    {% if formset.non_form_errors %}
        <b>Please correct the error below:</b>
        <ul>
            {% for error in formset.non_form_errors %}
            <li><p style="color: red;"> {{ error }} </p></li>
        {% endfor %}
        </ul>
dreamzboy
  • 795
  • 15
  • 31

2 Answers2

1

You can declare a variable that is filled with the errors, then display it once at the end.

class BaseNameFormSet (BaseFormSet):
    ...
    ...
    ...

    if (firstname in firstnames) or (lastname in lastnames):
         error_msg='First or last name must be unique'
    if (firstname == '') or (lastname == ''):
        error_msg+='<br>Both first and last name must be filled out'
    if error_msg:
        raise forms.ValidationError(error_msg)
Alex Lord Mordor
  • 2,890
  • 7
  • 27
  • 47
1

If you are able to upgrade to Django 1.7, you can raise multiple errors by passing a list to the ValidationError constructor.

errors = []
if (firstname in firstnames) or (lastname in lastnames):
    errors.append('First or last name must be unique')
if (firstname == '') or (lastname == ''):
    errors.append('Both first and last name must be filled out')
if errors:
    raise ValidationError(errors)

Note that you shouldn't usually have to do checks like if firstname == '', just make firstname a required field. You might find this question about making a forms in a formset required to be useful.

Community
  • 1
  • 1
Alasdair
  • 298,606
  • 55
  • 578
  • 516
  • Upgrading to Django 1.7 won't be an easy option at this point. I've seen documentation that uses an error list but thought there was a better way. Also, the "required" field in Django doesn't work quite right when you have an empty string. It will pass as a valid form; hence, I had to do it this way. I appreciate your suggestion nevertheless. – dreamzboy Jan 16 '15 at 00:04
  • The above method has worked for me without upgrading. Thank you for your help! – dreamzboy Jan 16 '15 at 00:16
  • For bound forms, `CharFields` *will* give errors for empty strings. In your case, because you're using a formset, you need the checks for completely empty, unbound forms. I've added a link to a question that you might find useful. Note that support for Django 1.6 will end once Django 1.8 is released ([perhaps in April](https://code.djangoproject.com/wiki/Version1.8Roadmap)), so you should think about upgrading soon. – Alasdair Jan 16 '15 at 10:56
  • Thanks again Alasdair. I've ran into that link before but felt that it didn't fit in my case. Now it does so I'll give it another look. Cheers! – dreamzboy Jan 21 '15 at 06:02