misc: add inputmode=number attribute on zipcode/etc. widgets (#45152)

This commit is contained in:
Frédéric Péters 2020-07-15 19:10:48 +02:00
parent 980fc9f36d
commit f8be9e5c56
2 changed files with 52 additions and 8 deletions

View File

@ -687,6 +687,11 @@ def test_wcsextrastringwidget_builtin_validation():
mock_form_submission(req, widget, {'test': '12345'})
assert not widget.has_error()
# and check it gets a special HTML inputmode
widget = WcsExtraStringWidget('test', value='foo', required=False)
widget.field = fakefield
assert 'inputmode="number"' in str(widget.render())
widget = WcsExtraStringWidget('test', value='foo', required=False)
widget.field = fakefield
mock_form_submission(req, widget, {'test': '1234'})

View File

@ -201,6 +201,8 @@ def string_render_content(self):
attrs['autocomplete'] = self.prefill_attributes['autocomplete']
if self.attrs:
attrs.update(self.attrs)
if getattr(self, 'inputmode', None):
attrs['inputmode'] = self.inputmode
return htmltag("input", xml_end=True, type=self.HTML_TYPE, name=self.name,
value=self.value, **attrs)
StringWidget.render_content = string_render_content
@ -950,14 +952,44 @@ class ValidationCondition(Condition):
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)'), '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'}),
('nir-fr', {'title': N_('NIR (France)'), 'function': 'validate_nir'}),
('iban', {'title': N_('IBAN'), 'function': 'validate_iban'}),
('digits', {
'title': N_('Digits'),
'regex': r'\d+',
'html_inputmode': 'number'}
),
('phone', {
'title': N_('Phone Number'),
'regex': r'\+?[-\(\)\d\.\s/]+',
'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': r'\d{5}',
'html_inputmode': 'number'}
),
('siren-fr', {
'title': N_('SIREN Code (France)'),
'function': 'validate_siren',
'html_inputmode': 'number'}
),
('siret-fr', {
'title': N_('SIRET Code (France)'),
'function': 'validate_siret',
'html_inputmode': 'number'}
),
('nir-fr', {
'title': N_('NIR (France)'),
'function': 'validate_nir'}
),
('iban', {
'title': N_('IBAN'),
'function': 'validate_iban'}
),
('regex', {'title': N_('Regular Expression')}),
('django', {'title': N_('Django Condition')}),
])
@ -1036,6 +1068,12 @@ class ValidationWidget(CompositeWidget):
return validation_method.get('html_input_type')
return 'text'
@classmethod
def get_html_inputmode(cls, validation):
validation_method = cls.validation_methods.get(validation['type'])
if validation_method and validation_method.get('html_inputmode'):
return validation_method.get('html_inputmode')
class WcsExtraStringWidget(StringWidget):
field = None
@ -1049,6 +1087,7 @@ class WcsExtraStringWidget(StringWidget):
def render_content(self):
if self.field and self.field.validation:
self.HTML_TYPE = ValidationWidget.get_html_input_type(self.field.validation)
self.inputmode = ValidationWidget.get_html_inputmode(self.field.validation)
return super(WcsExtraStringWidget, self).render_content()
def _parse(self, request):