Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8982841
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T20:40:49+00:00 2026-06-15T20:40:49+00:00

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

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T20:40:50+00:00Added an answer on June 15, 2026 at 8:40 pm

    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
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some MET data I want to validate which would look something like
I have a form which I want to be 'resusable' for a variety of
I have a simple task which I'm failing at. I want to take this
I have a PHP form which posts any number of records for the following:
Long story short, I have a form for which I want the id field
I am trying to validate Zend_Form which has several optional fields and I want
In my application I have a lot of forms that I want to validate.
I have a numberDecimal EditText which I want to validate using a regular expression.
I'm trying to validate a number by it's length. This number has to have
I want to write a function which can validate a given value (passed as

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.