Skip to content

is_valid() does not handle clean() not returning a value #75

Description

@pknowles

There is a straight self.cleaned_data = self.clean() assignment in is_valid() here: https://github.com/fusionbox/django-betterforms/blob/2.0.0/betterforms/multiform.py#L103

However, the docs allow for clean() functions to not return anything, in which case the existing .cleaned_data should be used: https://docs.djangoproject.com/en/4.2/ref/forms/validation/

The call to super().clean() in the example code ensures that any validation logic in parent classes is maintained. If your form inherits another that doesn’t return a cleaned_data dictionary in its clean() method (doing so is optional), then don’t assign cleaned_data to the result of the super() call and use self.cleaned_data instead:

def clean(self):
    super().clean()
    cc_myself = self.cleaned_data.get("cc_myself")
    ...

Related: https://stackoverflow.com/questions/46606284/returning-cleaned-data-when-overwriting-clean-method-in-django-model-forms

It looks like is_valid was originally implemented this way but was changed in 10a9c6c. Maybe this change should be partially reverted. A side note with that change, if an app does form.cleaned_data = {} the existing @cleaned_data.setter will not update any child forms, when I'd expect unreferenced forms' cleaned_data to be cleared.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions