From cafa5aefe952e138d8dbc34fc481a43c627d404e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20P=C3=A9ters?= Date: Tue, 2 Aug 2022 16:05:01 +0200 Subject: [PATCH] forms: do not fail normalization on 'none' validation (#67885) --- wcs/qommon/form.py | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/wcs/qommon/form.py b/wcs/qommon/form.py index 1bded3096..8a9402c01 100644 --- a/wcs/qommon/form.py +++ b/wcs/qommon/form.py @@ -1198,7 +1198,7 @@ class ValidationWidget(CompositeWidget): 'title': _('IBAN'), 'function': 'validate_iban', 'error_message': _('Invalid IBAN'), - 'parse': lambda v: v.upper().strip().replace(' ', ''), + 'normalize_function': lambda v: v.upper().strip().replace(' ', ''), }, ), ('regex', {'title': _('Regular Expression')}), @@ -1328,6 +1328,13 @@ class ValidationWidget(CompositeWidget): return validation.get('value') return None + @classmethod + def get_normalize_function(cls, validation): + validation_method = cls.validation_methods.get(validation['type']) + if validation_method and validation_method.get('normalize_function'): + return validation_method['normalize_function'] + return lambda x: x # identity + @classmethod def get_html_input_type(cls, validation): validation_method = cls.validation_methods.get(validation['type']) @@ -1371,9 +1378,8 @@ class WcsExtraStringWidget(StringWidget): self.error = self.validation_function_error_message or _('invalid value') if self.field and self.value and not self.error and self.field.validation: - parser = ValidationWidget.validation_methods[self.field.validation['type']].get('parse') - if parser: - self.value = parser(self.value) + normalize = ValidationWidget.get_normalize_function(self.field.validation) + self.value = normalize(self.value) class DateWidget(StringWidget):