DEV Community

Discussion on: Django inline formsets with Class-based views and crispy forms

Collapse
 
mustafamond profile image
Mustafamond

Thanks again for your post it was a great help! I am having trouble with validation of the formset, though - if the main form does not have any errors but one of the forms in the formset does, the form still submits as if there were no errors, the main form saves, but the formset does not. Validation errors in the formset do not get displayed. In the CreateView, the form_valid method is as follows:

def form_valid(self, form):
        context = self.get_context_data()
        titles = context['titles']
        with transaction.atomic():
            form.instance.created_by = self.request.user
            self.object = form.save()
            if titles.is_valid():
                titles.instance = self.object
                titles.save()
        return super(CollectionCreate, self).form_valid(form)

It checks if titles is valid, but does nothing if titles is not valid. Would this be the place to add an else:?

if titles.is_valid():
    save the data
else:
   refresh the form showing errors in the formset

Or does that logic belong earlier in the form processing flow?

Thank you for any ideas on how to resolve this.