From 6df27813cce923575384867ef2e7513e1ba59cdc Mon Sep 17 00:00:00 2001 From: Agate Date: Thu, 7 Jul 2022 17:59:48 +0200 Subject: [PATCH] widgets: more flexible validation of IBAN fields (#67120) --- tests/test_widgets.py | 4 +++- wcs/qommon/form.py | 6 ++++++ wcs/qommon/misc.py | 1 + 3 files changed, 10 insertions(+), 1 deletion(-) diff --git a/tests/test_widgets.py b/tests/test_widgets.py index a6ab47cdc..30648381f 100644 --- a/tests/test_widgets.py +++ b/tests/test_widgets.py @@ -1085,6 +1085,7 @@ def test_wcsextrastringwidget_iban_validation(): # regular cases for iban in [ 'BE71 0961 2345 6769', # Belgium + 'be71 0961 2345 6769', # Lowercase 'FR76 3000 6000 0112 3456 7890 189', # France 'FR27 2004 1000 0101 2345 6Z02 068', # France (having letter) 'DE91 1000 0000 0123 4567 89', # Germany @@ -1099,6 +1100,8 @@ def test_wcsextrastringwidget_iban_validation(): widget.field = fakefield mock_form_submission(req, widget, {'test': iban.replace(' ', '')}) assert not widget.has_error() + widget._parse(req) + assert widget.value == iban.upper().replace(' ', '') # failing cases for iban in [ @@ -1113,7 +1116,6 @@ def test_wcsextrastringwidget_iban_validation(): 'FR76', 'FR76 0000 0000 0000 0000 0000 000', 'FR76 1234 4567', - 'Fr76 3000 6000 0112 3456 7890 189', ]: widget = WcsExtraStringWidget('test', value='foo', required=False) widget.field = fakefield diff --git a/wcs/qommon/form.py b/wcs/qommon/form.py index 74c7f567c..46f1587a3 100644 --- a/wcs/qommon/form.py +++ b/wcs/qommon/form.py @@ -1193,6 +1193,7 @@ class ValidationWidget(CompositeWidget): 'title': _('IBAN'), 'function': 'validate_iban', 'error_message': _('Invalid IBAN'), + 'parse': lambda v: v.upper().strip().replace(' ', ''), }, ), ('regex', {'title': _('Regular Expression')}), @@ -1364,6 +1365,11 @@ class WcsExtraStringWidget(StringWidget): if self.value and self.validation_function and not self.validation_function(self.value): 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) + class DateWidget(StringWidget): '''StringWidget which checks the value entered is a correct date''' diff --git a/wcs/qommon/misc.py b/wcs/qommon/misc.py index db63cbea7..03f693e8a 100644 --- a/wcs/qommon/misc.py +++ b/wcs/qommon/misc.py @@ -1001,6 +1001,7 @@ def validate_iban(string_value): '''https://fr.wikipedia.org/wiki/International_Bank_Account_Number''' if not string_value: return False + string_value = string_value.upper().strip().replace(' ', '') country_code = string_value[:2] iban_key = string_value[2:4] bban = string_value[4:] -- 2.36.1