views:

177

answers:

0

I have a set of forms (lets say formA,formB,formC) on a page that collects data and exposes different fields depending on which form is used. These forms have different underlying processing logic (and client-side logic) but have the same model

If formA is submitted,I noticed that a form.validationerror raised in formA's clean method then runs the clean method of formB and formC also and displays errors from the 3 forms, even though i just wanted to validate formA. Is this a bug or i shouldnt be using multiple modelforms for one model?

EDIT:

If FormA raises a validationerror, the clean function of both FormB and FormC is called also. You end up getting validation errors of all the 3 forms in your template.

##################### form.py ######################

from myobject.models import mymodel


class FormA(forms.ModelForm):

    ......

    def __init__(self, user=None, *args, **kwargs):
        self.user = user
        super(FormA, self).__init__(*args, **kwargs)       

    def clean(self):
        .....
        if 'url' not in self.cleaned_data:
            return forms.ValidationError(_("cant submit without a valid URL"))
        return self.cleaned_data


    class Meta:
        model = mymodel

class FormB(forms.ModelForm):

    ....
    def __init__(self, user=None, *args, **kwargs):
        self.user = user
        super(FormB, self).__init__(*args, **kwargs)

    def clean(self):
        ........       
        return self.cleaned_data

    class Meta:
        model = mymodel

class FormC(forms.ModelForm):

    .......

    def __init__(self, user=None, *args, **kwargs):
        self.user = user
        super(FormC, self).__init__(*args, **kwargs)

    def clean(self):
        ........ 
        return self.cleaned_data

    class Meta:
        model = mymodel



##################### view.py ######################

def update(request, username):

    if request.POST["action"] == "FormA":
       form_A = FormA(request.user, request.POST,request=request)
       if form_A.is_valid():
            .....