From af5cf6fa8a5a801dba7ac13fe0b1f78e36d210a4 Mon Sep 17 00:00:00 2001
From: Bryan Brancotte <bryan.brancotte@pasteur.fr>
Date: Wed, 9 Apr 2025 17:21:00 +0200
Subject: [PATCH] validate ip form properties, post process
 number_of_subparts_str when typed not separable

---
 src/InSillyCloWeb/assemblies/forms.py | 14 ++++++++++----
 1 file changed, 10 insertions(+), 4 deletions(-)

diff --git a/src/InSillyCloWeb/assemblies/forms.py b/src/InSillyCloWeb/assemblies/forms.py
index f948f60..5bc05af 100644
--- a/src/InSillyCloWeb/assemblies/forms.py
+++ b/src/InSillyCloWeb/assemblies/forms.py
@@ -214,11 +214,17 @@ class InputPartForm(forms.ModelForm):
                 css_class='num-sub-parts-str',
             ),
         )
+        self.fields['nb_subpart_allowed'].label = mark_safe(self.fields['nb_subpart_allowed'].label + asterisk)
 
-    # def clean(self):
-    #     cleaned_data = super().clean()
-    #     cleaned_data['assembly'] = self.assembly
-    #     return cleaned_data
+    def clean(self):
+        cleaned_data = super().clean()
+        if self.errors:
+            return cleaned_data
+        if cleaned_data["is_typed"] and not cleaned_data["is_separable"]:
+            cleaned_data["number_of_subparts_str"] = f"[1]"
+        if len(cleaned_data["nb_subpart_allowed"]) == 0 and cleaned_data["is_separable"] and cleaned_data["is_typed"]:
+            self.add_error('nb_subpart_allowed', _('You have to specify at least one configuration'))
+        return cleaned_data
 
 
 InputPartFormFormSet = formset_factory(
-- 
GitLab