mirror of
https://github.com/netbox-community/netbox.git
synced 2026-02-05 23:06:25 -06:00
Refactor form validation
This commit is contained in:
@@ -144,11 +144,20 @@ class FrontPortFormMixin(forms.Form):
|
|||||||
def clean(self):
|
def clean(self):
|
||||||
super().clean()
|
super().clean()
|
||||||
|
|
||||||
# Count of selected rear port & position pairs much match the assigned number of positions
|
# Check that the total number of FrontPorts and positions matches the selected number of RearPort:position
|
||||||
if len(self.cleaned_data['rear_ports']) != self.cleaned_data['positions']:
|
# mappings. Note that `name` will be a list under FrontPortCreateForm, in which cases we multiply the number of
|
||||||
|
# FrontPorts being creation by the number of positions.
|
||||||
|
positions = self.cleaned_data['positions']
|
||||||
|
frontport_count = len(self.cleaned_data['name']) if type(self.cleaned_data['name']) is list else 1
|
||||||
|
rearport_count = len(self.cleaned_data['rear_ports'])
|
||||||
|
if frontport_count * positions != rearport_count:
|
||||||
raise forms.ValidationError({
|
raise forms.ValidationError({
|
||||||
'rear_ports': _(
|
'rear_ports': _(
|
||||||
"The number of rear port/position pairs selected must match the number of positions assigned."
|
"The total number of front port positions ({frontport_count}) must match the selected number of "
|
||||||
|
"rear port positions ({rearport_count})."
|
||||||
|
).format(
|
||||||
|
frontport_count=frontport_count,
|
||||||
|
rearport_count=rearport_count
|
||||||
)
|
)
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|||||||
@@ -249,24 +249,6 @@ class FrontPortCreateForm(ComponentCreateForm, model_forms.FrontPortForm):
|
|||||||
'device', 'module', 'type', 'color', 'positions', 'mark_connected', 'description', 'owner', 'tags',
|
'device', 'module', 'type', 'color', 'positions', 'mark_connected', 'description', 'owner', 'tags',
|
||||||
]
|
]
|
||||||
|
|
||||||
def clean(self):
|
|
||||||
super(NetBoxModelForm, self).clean()
|
|
||||||
|
|
||||||
# Check that the number of FrontPorts to be created matches the selected number of RearPorts
|
|
||||||
positions = self.cleaned_data['positions']
|
|
||||||
frontport_count = len(self.cleaned_data['name'])
|
|
||||||
rearport_count = len(self.cleaned_data['rear_ports'])
|
|
||||||
if frontport_count * positions != rearport_count:
|
|
||||||
raise forms.ValidationError({
|
|
||||||
'rear_ports': _(
|
|
||||||
"The number of front ports to be created ({frontport_count}) must match the selected number of "
|
|
||||||
"rear port positions ({rearport_count})."
|
|
||||||
).format(
|
|
||||||
frontport_count=frontport_count,
|
|
||||||
rearport_count=rearport_count
|
|
||||||
)
|
|
||||||
})
|
|
||||||
|
|
||||||
def get_iterative_data(self, iteration):
|
def get_iterative_data(self, iteration):
|
||||||
positions = self.cleaned_data['positions']
|
positions = self.cleaned_data['positions']
|
||||||
offset = positions * iteration
|
offset = positions * iteration
|
||||||
|
|||||||
Reference in New Issue
Block a user