0001-forms-use-type-tel-for-phone-number-input-41077.patch
tests/test_widgets.py | ||
---|---|---|
655 | 655 |
assert widget.has_error() |
656 | 656 | |
657 | 657 | |
658 |
def test_wcsextrastringwidget_phone_fr(): |
|
659 |
class FakeField: |
|
660 |
pass |
|
661 |
fakefield = FakeField() |
|
662 | ||
663 |
# check validation |
|
664 |
fakefield.validation = {'type': 'phone-fr'} |
|
665 |
widget = WcsExtraStringWidget('test', value='foo', required=False) |
|
666 |
widget.field = fakefield |
|
667 |
mock_form_submission(req, widget, {'test': '0123456789'}) |
|
668 |
assert not widget.has_error() |
|
669 | ||
670 |
widget = WcsExtraStringWidget('test', value='foo', required=False) |
|
671 |
widget.field = fakefield |
|
672 |
mock_form_submission(req, widget, {'test': 'az'}) |
|
673 |
assert widget.has_error() |
|
674 | ||
675 |
widget = WcsExtraStringWidget('test', value='foo', required=False) |
|
676 |
widget.field = fakefield |
|
677 |
mock_form_submission(req, widget, {'test': '0123'}) |
|
678 |
assert widget.has_error() |
|
679 | ||
680 |
# and check it gets a special HTML input type |
|
681 |
assert 'type="tel"' in str(widget.render()) |
|
682 | ||
683 | ||
658 | 684 |
def test_wcsextrastringwidget_siren_validation(): |
659 | 685 |
class FakeField: pass |
660 | 686 |
fakefield = FakeField() |
wcs/qommon/form.py | ||
---|---|---|
895 | 895 |
class ValidationWidget(CompositeWidget): |
896 | 896 |
validation_methods = collections.OrderedDict([ |
897 | 897 |
('digits', {'title': N_('Digits'), 'regex': '\d+'}), |
898 |
('phone-fr', {'title': N_('Phone Number (France)'), 'regex': '0[\d\.\s]{9}'}), |
|
898 |
('phone-fr', {'title': N_('Phone Number (France)'), 'regex': '0[\d\.\s]{9}', 'html_input_type': 'tel'}),
|
|
899 | 899 |
('zipcode-fr', {'title': N_('Zip Code (France)'), 'regex': '\d{5}'}), |
900 | 900 |
('siren-fr', {'title': N_('SIREN Code (France)'), 'function': 'validate_siren'}), |
901 | 901 |
('siret-fr', {'title': N_('SIRET Code (France)'), 'function': 'validate_siret'}), |
... | ... | |
973 | 973 |
return validation.get('value') |
974 | 974 |
return None |
975 | 975 | |
976 |
@classmethod |
|
977 |
def get_html_input_type(cls, validation): |
|
978 |
validation_method = cls.validation_methods.get(validation['type']) |
|
979 |
if validation_method and validation_method.get('html_input_type'): |
|
980 |
return validation_method.get('html_input_type') |
|
981 |
return 'text' |
|
982 | ||
976 | 983 | |
977 | 984 |
class WcsExtraStringWidget(StringWidget): |
978 | 985 |
field = None |
... | ... | |
983 | 990 |
if self.prefill_attributes and 'geolocation' in self.prefill_attributes: |
984 | 991 |
get_response().add_javascript(['qommon.geolocation.js']) |
985 | 992 | |
993 |
def render_content(self): |
|
994 |
if self.field and self.field.validation: |
|
995 |
self.HTML_TYPE = ValidationWidget.get_html_input_type(self.field.validation) |
|
996 |
return super(WcsExtraStringWidget, self).render_content() |
|
997 | ||
986 | 998 |
def _parse(self, request): |
987 | 999 |
StringWidget._parse(self, request) |
988 | 1000 |
if self.field and self.field.validation and self.value is not None: |
989 |
- |