首页 文章

在Django中不提高ValidationError使表单无效

提问于
浏览
0

我希望一个表单无效,而不会在任何形式的's or form'字段's clean methods. The reason for this is that the form is the 2832272 for a set of 2832273 , 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'的错误列表中引发ValidationError(避免在超级表单中显示错误消息的 non_field_errors 是我的主要动机 . )一种解决方法对我而言将在视图中检查 is_valid 的超级形式及其子表单,但我更喜欢只检查超级表单并使其 is_valid 返回 False ,即使其字段有效,但当其一个或多个子表单返回时 Falseis_valid . 谢谢 .

1 回答

  • 1

    为什么不重写is_valid方法?

    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)
    

相关问题