How to make all forms in django formset required? I tried to validate presence of all fields in cleaned_data
overriding formset's clean() method but it just fails silently without any error displayed.
Thanks!
Source code:
class BaseScheduleForm(forms.ModelForm):
def __init__(self, *args, **kwargs):
super(BaseScheduleForm, self).__init__(*args, **kwargs)
self.fields['day'].widget = forms.HiddenInput()
self.fields['user'].widget = forms.HiddenInput()
class Meta:
model = Schedule
def clean_end_time(self):
start_time = self.cleaned_data.get('start_time')
end_time = self.cleaned_data['end_time']
if start_time and end_time:
if end_time <= start_time:
raise forms.ValidationError("End time must be later that start time.")
return end_time
class BaseScheduleFormset(forms.models.BaseModelFormSet):
def __init__(self, *args, **kwargs):
super(BaseScheduleFormset, self).__init__(*args, **kwargs)
for number, weekday in enumerate(WEEKDAYS):
self.forms[number].day_name = weekday[1]
def clean(self):
raise forms.ValidationError('You must specify schedule for the whole week')
ScheduleFormset = forms.models.modelformset_factory(Schedule, extra=7, max_num=7,
form=BaseScheduleForm, formset=BaseScheduleFormset)
There are 7 forms each for one day and all of them must be filled to be valid. In example above I just tried to raise error in clean. is_valid()
became False, but no errors were displayed.