Django formsets: make first required?

前端 未结 4 472
独厮守ぢ
独厮守ぢ 2020-11-28 04:05

These formsets are exhibiting exactly the opposite behavior that I want.

My view is set up like this:

def post(request): # TODO: handle vehi         


        
4条回答
  •  孤城傲影
    2020-11-28 04:39

    Well... this makes the first form required.

    class RequiredFormSet(BaseFormSet):
        def clean(self):
            if any(self.errors):
                return
            if not self.forms[0].has_changed():
                raise forms.ValidationError('Please add at least one vehicle.') 
    

    Only "problem" is that if there are 0 forms, then the clean method doesn't seem to get called at all, so I don't know how to check if there are 0. Really...this should never happen though (except that my JS has a bug in it, allowing you to remove all the forms).

提交回复
热议问题