views:

259

answers:

1

Hello,

I would like a form to be invalid without raising a ValidationError in any of the form's or form's field's clean methods. The reason for this is that the form is the "super form" for a set of "sub forms", and I want the super form to be invalid when any of its subforms are invalid. But this invalidity does not entail raising a ValidationError in the super form, and in fact I do not want to raise a ValidationError in the super form because I do not want any error message to appear in the super form's error lists (avoiding the display of an error message in the super form's non_field_errors is my main motivation here.) One way around this for me would be to check both the super form and its sub forms for is_valid in the view, but I prefer to check only the super form and have its is_valid return False even when its fields are valid, but when one or more of its sub forms returns False to is_valid. Thank you.

+1  A: 

Why don't you override the is_valid method?

class SuperForm(forms.Form):
  def is_valid(self):
    return forms.Form.is_valid(self) and all(form.is_valid() for form in self.sub_forms)
Alexander Ljungberg
I think this is the correct option. I was not aware of the `all` function in python; that function will cleanup my code in several places, I think. Thank you!
DGGenuine
Did you mean to call `forms.Form.is_valid(self)`? What about `super(SuperForm, self).is_valid()`?
DGGenuine