Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django custom validation of multiple fields

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.

like image 395
Darwin Tech Avatar asked Dec 12 '12 17:12

Darwin Tech


1 Answers

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
like image 148
mVChr Avatar answered Nov 15 '22 00:11

mVChr