ソースを参照

Make option group required for option and multi-option attribute types.

master
Victor Munene 4年前
コミット
2fe4036bf7

+ 6
- 0
src/oscar/apps/dashboard/catalogue/forms.py ファイルの表示

@@ -397,6 +397,12 @@ class ProductAttributesForm(forms.ModelForm):
397 397
 
398 398
         return code
399 399
 
400
+    def clean(self):
401
+        attr_type = self.cleaned_data.get('type')
402
+        option_group = self.cleaned_data.get('option_group')
403
+        if attr_type in [ProductAttribute.OPTION, ProductAttribute.MULTI_OPTION] and not option_group:
404
+            self.add_error('option_group', _('An option group is required'))
405
+
400 406
     class Meta:
401 407
         model = ProductAttribute
402 408
         fields = ["name", "code", "type", "option_group", "required"]

+ 7
- 1
src/oscar/static_src/oscar/js/oscar/dashboard.js ファイルの表示

@@ -354,7 +354,13 @@ var oscar = (function(o, $) {
354 354
             toggleOptionGroup: function(type_select){
355 355
                 var option_group_select = $('#' + type_select.attr('id').replace('type', 'option_group'));
356 356
                 var v = type_select.val();
357
-                option_group_select.parent().parent().toggle(v === 'option' || v === 'multi_option');
357
+                var showOptionGroup = v === 'option' || v === 'multi_option';
358
+                option_group_select.parent().parent().toggle(showOptionGroup);
359
+                if(showOptionGroup){
360
+                    option_group_select.attr('required', 'required');
361
+                }else{
362
+                    option_group_select.attr('required', null);
363
+                }
358 364
             }
359 365
         },
360 366
         ranges: {

+ 15
- 0
tests/integration/dashboard/test_catalogue_form.py ファイルの表示

@@ -39,3 +39,18 @@ class TestCreateProductAttributeForm(TestCase):
39 39
 
40 40
         # check that code is not None or empty string
41 41
         self.assertTrue(product_attribute.code)
42
+
43
+    def test_option_group_required_if_attribute_is_option_or_multi_option(self):
44
+        option_form = forms.ProductAttributesForm(data={
45
+            "name": "Attr",
46
+            "type": "option"
47
+        })
48
+        self.assertFalse(option_form.is_valid())
49
+        self.assertEqual(option_form.errors, {'option_group': ['An option group is required']})
50
+
51
+        multi_option_form = forms.ProductAttributesForm(data={
52
+            "name": "Attr",
53
+            "type": "option"
54
+        })
55
+        self.assertFalse(multi_option_form.is_valid())
56
+        self.assertEqual(multi_option_form.errors, {'option_group': ['An option group is required']})

読み込み中…
キャンセル
保存