Skip to content

Saleor Core: the high performance, composable, headless commerce API.

Add more detailed validation in dashboard

Artur Smęt1615fafde907
diff --git a/saleor/dashboard/discount/forms.py b/saleor/dashboard/discount/forms.py
index b0a8580d15..8bc0820a0c 100644
--- a/saleor/dashboard/discount/forms.py
+++ b/saleor/dashboard/discount/forms.py
@@ -1,4 +1,5 @@
from django import forms
+from django.core.exceptions import NON_FIELD_ERRORS
from django.utils.translation import pgettext_lazy
from ...product.models import Discount
@@ -11,11 +12,23 @@ class DiscountForm(forms.ModelForm):
def clean(self):
cleaned_data = super(DiscountForm, self).clean()
- type = cleaned_data['type']
+ discount_type = cleaned_data['type']
+ apply_on = cleaned_data['apply_on']
value = cleaned_data['value']
- if type == Discount.PERCENTAGE and value > 100:
+ required_msg = pgettext_lazy('discount error', 'This field is required')
+ if discount_type == Discount.PERCENTAGE and value > 100:
self.add_error('value', pgettext_lazy('discount error',
'Percentage discount '
'cannot be higher than 100%'))
+ if (apply_on == Discount.APPLY_ON_PRODUCTS and not
+ cleaned_data['products']):
+ self.add_error('products', required_msg)
+ elif (apply_on == Discount.APPLY_ON_CATEGORIES and not
+ cleaned_data['categories']):
+ self.add_error('categories', required_msg)
+ elif apply_on == Discount.APPLY_ON_BOTH and not (
+ cleaned_data['products'] or cleaned_data['categories']):
+ self.add_error('products', required_msg)
+ self.add_error('categories', required_msg)
# TODO: Implement cost price checks
return cleaned_data