tags:

views:

6

answers:

1

Hi

I have a form which has two fields for Integers:

class DemoForm(forms.Form):
b_one = forms.IntegerField(
    error_messages={
       'required':'Please enter a valid number.'
    },
    label = 'NumberOne',
    required = True,
    help_text = 'e.g. 266492'
)
b_two = forms.IntegerField(
    error_messages={
       'required':'Please enter a valid number.'
    },
    label = 'NumberTwo',
    required = True,
    help_text = 'e.g. 262865',
)

and I am validating these fields as

def clean_b_one(self):
    self.validate_form(self.cleaned_data['b_one'])

def clean_b_two(self):
    self.validate_form(self.cleaned_data['b_two'])

Now what I want to do is in validate_form I check, if these numbers exists in database, else raising forms.ValidationError

But what I also want to do some other validations when these form fields are valid, basically some check on the form based on input and raise some custom errors, where can I add logic? or what is the best way of doing it?

A: 

You can do individual field verifying in clean_b_one like you have, and raise ValidationErrors if something doesn't fit. Or override the clean method to do cross-field checking. General documentation to be found here.

eruciform