From 936393db3699b049a7d0123d5fc14acffa0eecbb Mon Sep 17 00:00:00 2001 From: Marcos Amorim Date: Wed, 9 Nov 2016 16:21:45 +0000 Subject: [PATCH] Verify if min/max are a valid range and if initial is inside that range --- .../form_elements/fields/decimal/forms.py | 29 +++++++++++++++++++ .../form_elements/fields/float/forms.py | 29 +++++++++++++++++++ .../form_elements/fields/integer/forms.py | 29 +++++++++++++++++++ 3 files changed, 87 insertions(+) diff --git a/src/fobi/contrib/plugins/form_elements/fields/decimal/forms.py b/src/fobi/contrib/plugins/form_elements/fields/decimal/forms.py index 9f955199..468bc902 100644 --- a/src/fobi/contrib/plugins/form_elements/fields/decimal/forms.py +++ b/src/fobi/contrib/plugins/form_elements/fields/decimal/forms.py @@ -91,3 +91,32 @@ class DecimalInputForm(forms.Form, BaseFormFieldPluginForm): attrs={'class': theme.form_element_html_class} ) ) + + def clean(self): + """Validating the values.""" + super(DecimalInputForm, self).clean() + + max_value = self.cleaned_data['max_value'] + min_value = self.cleaned_data['min_value'] + initial = self.cleaned_data['initial'] + + if ( + max_value is not None and min_value is not None and + max_value < min_value + ): + self.add_error( + 'max_value', + _("`max_value` should be > than `min_value`.") + ) + + if max_value is not None and initial and max_value < initial: + self.add_error( + 'initial', + _("`max_value` should be >= than `initial`.") + ) + + if min_value is not None and initial and min_value > initial: + self.add_error( + 'min_value', + _("`initial` should be >= than `min_value`.") + ) diff --git a/src/fobi/contrib/plugins/form_elements/fields/float/forms.py b/src/fobi/contrib/plugins/form_elements/fields/float/forms.py index ec469d87..a52c3eb7 100644 --- a/src/fobi/contrib/plugins/form_elements/fields/float/forms.py +++ b/src/fobi/contrib/plugins/form_elements/fields/float/forms.py @@ -77,3 +77,32 @@ class FloatInputForm(forms.Form, BaseFormFieldPluginForm): attrs={'class': theme.form_element_html_class} ) ) + + def clean(self): + """Validating the values.""" + super(FloatInputForm, self).clean() + + max_value = self.cleaned_data['max_value'] + min_value = self.cleaned_data['min_value'] + initial = self.cleaned_data['initial'] + + if ( + max_value is not None and min_value is not None and + max_value < min_value + ): + self.add_error( + 'max_value', + _("`max_value` should be > than `min_value`.") + ) + + if max_value is not None and initial and max_value < initial: + self.add_error( + 'initial', + _("`max_value` should be >= than `initial`.") + ) + + if min_value is not None and initial and min_value > initial: + self.add_error( + 'min_value', + _("`initial` should be >= than `min_value`.") + ) diff --git a/src/fobi/contrib/plugins/form_elements/fields/integer/forms.py b/src/fobi/contrib/plugins/form_elements/fields/integer/forms.py index 9d4ee8ed..47e9bb21 100644 --- a/src/fobi/contrib/plugins/form_elements/fields/integer/forms.py +++ b/src/fobi/contrib/plugins/form_elements/fields/integer/forms.py @@ -77,3 +77,32 @@ class IntegerInputForm(forms.Form, BaseFormFieldPluginForm): attrs={'class': theme.form_element_html_class} ) ) + + def clean(self): + """Validating the values.""" + super(IntegerInputForm, self).clean() + + max_value = self.cleaned_data['max_value'] + min_value = self.cleaned_data['min_value'] + initial = self.cleaned_data['initial'] + + if ( + max_value is not None and min_value is not None and + max_value < min_value + ): + self.add_error( + 'max_value', + _("`max_value` should be > than `min_value`.") + ) + + if max_value is not None and initial and max_value < initial: + self.add_error( + 'initial', + _("`max_value` should be >= than `initial`.") + ) + + if min_value is not None and initial and min_value > initial: + self.add_error( + 'min_value', + _("`initial` should be >= than `min_value`.") + )