Search code examples
djangodjango-formsdjango-validation

Django form validation, raise error on fieldset


I know how to raise an error on a specific field in my django admin form, but I would like the error to be raised on a fieldset. I currently have a list of check boxes in a fieldset and would like an error to be raised, not on a specific field (aka specific check box), but on the entire fieldset.

here is my django admin.py

class EventAdmin(admin.ModelAdmin):
    form = EventForm
    fieldsets = [
        (None,  {'fields': [
                                'approval_state',
                                'title',
                                'description'
                            ]
                }
        ),
        ('Group Owner', {'fields': [
                                        'grpOwner_vcoe',
                                        'grpOwner_cssi',
                                        'grpOwner_essc',
                                        'grpOwner_tmscc',
                                        'grpOwner_inmc',
                                        'grpOwner_cc7',
                                        'grpOwner_ias',
                                    ]
                        }
        ), ...

 class EventForm(forms.ModelForm):

    # Form validation
    def clean(self):
        # Collect data
        start = self.cleaned_data.get('start')
        end = self.cleaned_data.get('end')

        grpOwner_vcoe = self.cleaned_data.get('grpOwner_vcoe')
        grpOwner_cssi = self.cleaned_data.get('grpOwner_cssi')
        grpOwner_essc = self.cleaned_data.get('grpOwner_essc')
        grpOwner_tmscc = self.cleaned_data.get('grpOwner_tmscc')
        grpOwner_inmc = self.cleaned_data.get('grpOwner_inmc')
        grpOwner_cc7 = self.cleaned_data.get('grpOwner_cc7')
        grpOwner_ias = self.cleaned_data.get('grpOwner_ias')

        if not (grpOwner_vcoe or grpOwner_cssi or grpOwner_essc or grpOwner_tmscc or grpOwner_inmc or grpOwner_cc7 or grpOwner_ias):
            if not self._errors.has_key('Group Owner'):
                self._errors['Group Owner'] = ErrorList()
            self._errors['Group Owner'].append('Test')

        # Check start & end data
        if start > end:
            if not self._errors.has_key('start'):
                self._errors['start'] = ErrorList()
            self._errors['start'].append('Event start must occur before event end')


        return self.cleaned_data

But this doesn't, I know I can just raise it on each field but I find it much more elegant if I could do it around the fielset


Solution

  • Django forms don't have a concept of fieldsets, they belong to the ModelAdmin class. Therefore there isn't an established way to assign errors to a fieldset instead of a particular field.

    You could try overriding the admin templates, in particular, includes/fieldset.html. You could add some code to your form's clean method to make it easy to access the fieldset errors in the template.