diff --git a/tests/test_widgets.py b/tests/test_widgets.py index 6b60dfa6b..4b3e8aa39 100644 --- a/tests/test_widgets.py +++ b/tests/test_widgets.py @@ -698,6 +698,11 @@ def test_wcsextrastringwidget_phone_fr(): mock_form_submission(req, widget, {'test': '0123456789'}) assert not widget.has_error() + widget = WcsExtraStringWidget('test', value='foo', required=False) + widget.field = fakefield + mock_form_submission(req, widget, {'test': '01 23 45 67 89'}) + assert not widget.has_error() + widget = WcsExtraStringWidget('test', value='foo', required=False) widget.field = fakefield mock_form_submission(req, widget, {'test': 'az'}) @@ -708,6 +713,11 @@ def test_wcsextrastringwidget_phone_fr(): mock_form_submission(req, widget, {'test': '0123'}) assert widget.has_error() + widget = WcsExtraStringWidget('test', value='foo', required=False) + widget.field = fakefield + mock_form_submission(req, widget, {'test': '01234567890123'}) + assert widget.has_error() + # and check it gets a special HTML input type assert 'type="tel"' in str(widget.render()) diff --git a/wcs/qommon/form.py b/wcs/qommon/form.py index d436a6aa4..7d5f3163d 100644 --- a/wcs/qommon/form.py +++ b/wcs/qommon/form.py @@ -930,7 +930,7 @@ class ValidationWidget(CompositeWidget): validation_methods = collections.OrderedDict([ ('digits', {'title': N_('Digits'), 'regex': '\d+'}), ('phone', {'title': N_('Phone Number'), 'regex': r'\+?[-\(\)\d\.\s/]+', 'html_input_type': 'tel'}), - ('phone-fr', {'title': N_('Phone Number (France)'), 'regex': '0[\d\.\s]{9}', 'html_input_type': 'tel'}), + ('phone-fr', {'title': N_('Phone Number (France)'), 'function': 'validate_phone_fr', 'html_input_type': 'tel'}), ('zipcode-fr', {'title': N_('Zip Code (France)'), 'regex': '\d{5}'}), ('siren-fr', {'title': N_('SIREN Code (France)'), 'function': 'validate_siren'}), ('siret-fr', {'title': N_('SIRET Code (France)'), 'function': 'validate_siret'}), diff --git a/wcs/qommon/misc.py b/wcs/qommon/misc.py index 25585905f..8b44c4664 100644 --- a/wcs/qommon/misc.py +++ b/wcs/qommon/misc.py @@ -705,6 +705,13 @@ def is_ascii_digit(string_value): return string_value and all((x in '0123456789' for x in string_value)) +def validate_phone_fr(string_value): + if not re.match(r'^0[\d\.\s]+$', string_value): + # leading zero, then digits, dots, or spaces + return False + return len([x for x in string_value if is_ascii_digit(x)]) == 10 + + def validate_siren(string_value): return validate_luhn(string_value, length=9)