widgets: more flexible validation of IBAN fields (#67120)

This commit is contained in:
Agate 2022-07-07 17:59:48 +02:00
parent ff4c760daf
commit a9aef3861f
3 changed files with 11 additions and 1 deletions

View File

@ -1085,6 +1085,8 @@ def test_wcsextrastringwidget_iban_validation():
# regular cases
for iban in [
'BE71 0961 2345 6769', # Belgium
'be71 0961 2345 6769', # Lowercase
' BE71 0961 2345 6769 ', # Extra padding
'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 +1101,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(' ', '').strip()
# failing cases
for iban in [
@ -1113,7 +1117,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

View File

@ -1198,6 +1198,7 @@ class ValidationWidget(CompositeWidget):
'title': _('IBAN'),
'function': 'validate_iban',
'error_message': _('Invalid IBAN'),
'parse': lambda v: v.upper().strip().replace(' ', ''),
},
),
('regex', {'title': _('Regular Expression')}),
@ -1369,6 +1370,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'''

View File

@ -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:]