Fwd: Formset clean() method is not called when invoking Formset is_valid()

528 views
Skip to first unread message

A D

unread,
Aug 1, 2015, 4:58:18 PM8/1/15
to django-d...@googlegroups.com
Hi,

  Reading on the documentation regarding formsets' validation:

It seems that an overrided clean() method in a custom formset deriving from forms.BaseFormSet would be called when validating a formset (in particular, when explicitly calling Formset's is_valid() method).

Yet, it does not appear to be the case in this example:

    class CustomForm(forms.Form):
        title = forms.CharField()
    
    
    class CustomFormset(forms.BaseFormSet):
        def clean(self):
            raise forms.ValidationError("This error is always raised, displaying it is success for the test.")
    
    
    def formset_error(request):
        FormSetClass = formset_factory(CustomForm, formset=CustomFormset, extra=2)
        if request.method == 'POST':
            formset = FormSetClass(request.POST)
            if formset.is_valid():
                pass
        else:
            formset = FormSetClass()
    return render(request, "form.html", { 'formset': formset })

The ValidationError is not raised (as the clean() method is not invoked, as shown by a debugger).
Is this the expected behaviour ? Or should I report a bug ?

Thank you for reading,
  Ad


Tim Graham

unread,
Aug 1, 2015, 6:58:20 PM8/1/15
to Django developers (Contributions to Django itself)
I couldn't reproduce a problem. In your template, be sure to include {{ formset.non_form_errors }} to see that error.

For future reference, "Is this a bug?" questions should typically be directed to django-users, thanks!
Reply all
Reply to author
Forward
0 new messages