7

for which I want to validate a number of fields in a custom clean method.

I have this so far:

class ProjectInfoForm(forms.Form):
    module = forms.ModelChoiceField(
        queryset=Module.objects.all(),
    )
    piece = forms.CharField(
        widget=forms.Select(),
        required=False,
    )
    span = forms.IntegerField(
        max_value=100,
        initial=48
    )
    max_span = forms.IntegerField(
        max_value=100,
        initial=0
    )


    def clean(self):
        span = self.cleaned_data['span']
        max_span = self.cleaned_data['max_span']
        piece = self.cleaned_data.['piece']

        # validate piece 
        try:
            Piece.objects.get(pk=m)
        except Piece.DoesNotExist:
            raise forms.ValidationError(
                'Illegal Piece selected!'
            )
            self._errors["piece"] = "Please enter a valid model"

        # validate spans
        if span > max_span:
            raise forms.ValidationError(
                'Span must be less than or equal to Maximum Span'
            )
            self._errors["span"] = "Please enter a valid span"
        return self.cleaned_data

However, this only gives me one of the messages if both clauses invalidate. How can I get all the invalid messages. Also I do not get the field-specific messages - how do I include a message to be displayed for the specific field?

Any help much appreciated.

4 Answers 4

8

Store the errors and don't raise them until the end of the method:

def clean(self):
    span = self.cleaned_data['span']
    max_span = self.cleaned_data['max_span']
    piece = self.cleaned_data.['piece']
    error_messages = []

    # validate piece 
    try:
        Piece.objects.get(pk=m)
    except Piece.DoesNotExist:
        error_messages.append('Illegal Piece selected')
        self._errors["piece"] = "Please enter a valid model"

    # validate spans
    if span > max_span:
        error_messages.append('Span must be less than or equal to Maximum Span')
        self._errors["span"] = "Please enter a valid span"

    if len(error_messages):
        raise forms.ValidationError(' & '.join(error_messages))

    return self.cleaned_data
Sign up to request clarification or add additional context in comments.

5 Comments

Validation concerning only a single field should happen in a clean_FIELDNAME method on the form.
This answer works for me - is there something inherently wrong with this approach then?
@DarwinTech not really, but the clean method is conceptually meant for form-spanning (multi-field) validation. Single fields should have their own clean_FIELDNAME method.
ok. But my instance does validate multi-field does it not? I am validating span and max_span together.
Did you mean self.add_error(field_name, message) instead of self._error[field_name]=message? You should also consider the reason for having _ - private variable.
1

You should write a custom clean_FIELDNAME method in this case. That way, field centric validation errors can later be displayed as such when using {{form.errors}} in your template. The clean method o.t.h. is for validating logic that spans more than one field. Take a look through the link I posted above, everything you need to know about validating django forms is in there.

Comments

0

It happens because you are using raise. Try replace it by these two line in your code:

del self.cleaned_data['piece']

and

del self.cleaned_data['span']

Comments

0

It appears this has changed in later versions of Django (this seems to work in 2.1 and later):


from django import forms

class ContactForm(forms.Form):
    # Everything as before.
    ...

    def clean(self):
        cleaned_data = super().clean()
        cc_myself = cleaned_data.get("cc_myself")
        subject = cleaned_data.get("subject")

        if cc_myself and subject and "help" not in subject:
            msg = "Must put 'help' in subject when cc'ing yourself."
            self.add_error('cc_myself', msg)
            self.add_error('subject', msg)

https://docs.djangoproject.com/en/dev/ref/forms/validation/#raising-multiple-errors has more details.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.