[Django]-Django form field required conditionally

38👍

Check the Chapter on Cleaning and validating fields that depend on each other in the documentation.

The example given in the documentation can be easily adapted to your scenario:

def clean(self):
    cleaned_data = super(SignupFormExtra, self).clean()
    is_company = cleaned_data.get("is_company")
    nip = cleaned_data.get("NIP")
    if is_company and not nip:
        raise forms.ValidationError("NIP is a required field.")
    return cleaned_data
👤arie

Leave a comment