From d35acf0a013edc67918d8d83f164165918141246 Mon Sep 17 00:00:00 2001 From: Dillon Henschen Date: Thu, 25 May 2023 20:54:39 -0400 Subject: [PATCH] Clean CircuitTerminationForm data to avoid model ValidationErrors Associated with issue #11891 --- netbox/circuits/forms/model_forms.py | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/netbox/circuits/forms/model_forms.py b/netbox/circuits/forms/model_forms.py index 60e607c7f..e508767ea 100644 --- a/netbox/circuits/forms/model_forms.py +++ b/netbox/circuits/forms/model_forms.py @@ -169,3 +169,17 @@ class CircuitTerminationForm(NetBoxModelForm): labels = { 'termination_type': 'Termination Type', } + + def clean(self): + super().clean() + + termination_type = self.cleaned_data.get('termination_type') + + # Clear out the unselected termination type fields to prevent a ValidationError in the model. + # Otherwise, users would have to manually clear out the site or provider network field, before + # changing the termination type. That behavior may not be obvious or convenient for most users. + if termination_type != CircuitTerminationTypeChoices.SITE: + self.cleaned_data['site'] = None + + if termination_type != CircuitTerminationTypeChoices.PROVIDER_NETWORK: + self.cleaned_data['provider_network'] = None