Repository: netbox-community/netbox

Issue:
If you create a port in Alphanumeric ranges with not enough Rear ports, a server error will be displayed.
### NetBox version

v3.6.4

### Python version

3.8

### Steps to Reproduce

1. Create a rear port, with position 1.
2. When creating a front port, use Alphanumeric ranges to create a front port that is greater than or equal to the rear port.
3. Server error is confirmed.
![image](https://github.com/netbox-community/netbox/assets/33054826/f815c224-9558-4870-9968-8013d34da3d8)
![image](https://github.com/netbox-community/netbox/assets/33054826/88cc2b6a-4a52-4d04-9555-a13ee1a032bc)



### Expected Behavior

Expect that the creation process will not take place. If no rear port is selected, no creations can be made.

### Observed Behavior

The create process could be performed with no available rear ports.


Discussion:
> 3. Server error is confirmed.

To be clear, you would like a proper error to be displayed, not the generic Django error, correct? 

> The create process could be performed with no available rear ports.

It dooesn't actually create anything, it generates an erro, this is expected.
> To be clear, you would like a proper error to be displayed, not the generic Django error, correct?

Yes, I thought this error was not appropriate and reported it.

Resolving patch:
diff --git a/netbox/dcim/forms/object_create.py b/netbox/dcim/forms/object_create.py
--- a/netbox/dcim/forms/object_create.py
+++ b/netbox/dcim/forms/object_create.py
@@ -151,6 +151,23 @@ def __init__(self, *args, **kwargs):
                     )
         self.fields['rear_port'].choices = choices
 
+    def clean(self):
+
+        # Check that the number of FrontPortTemplates to be created matches the selected number of RearPortTemplate
+        # positions
+        frontport_count = len(self.cleaned_data['name'])
+        rearport_count = len(self.cleaned_data['rear_port'])
+        if frontport_count != rearport_count:
+            raise forms.ValidationError({
+                'rear_port': _(
+                    "The number of front port templates 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):
 
         # Assign rear port and position from selected set
@@ -291,6 +308,22 @@ def __init__(self, *args, **kwargs):
                     )
         self.fields['rear_port'].choices = choices
 
+    def clean(self):
+
+        # Check that the number of FrontPorts to be created matches the selected number of RearPort positions
+        frontport_count = len(self.cleaned_data['name'])
+        rearport_count = len(self.cleaned_data['rear_port'])
+        if frontport_count != rearport_count:
+            raise forms.ValidationError({
+                'rear_port': _(
+                    "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):
 
         # Assign rear port and position from selected set


Test patch: