misc: add common time validation (#45049)

This commit is contained in:
Lauréline Guérin 2023-01-19 16:38:30 +01:00 committed by Gitea
parent 71fd2038a6
commit 916eb7e1aa
2 changed files with 36 additions and 0 deletions

View File

@ -1192,6 +1192,33 @@ def test_wcsextrastringwidget_iban_validation():
assert widget.error == 'Invalid IBAN'
def test_wcsextrastringwidget_time():
class FakeField:
pass
fakefield = FakeField()
fakefield.validation = {'type': 'time'}
# check validation
for valid_case in ('00:00', '23:59'):
widget = WcsExtraStringWidget('test', value='foo', required=False)
widget.field = fakefield
mock_form_submission(req, widget, {'test': valid_case})
assert not widget.has_error()
for invalid_case in ('az', '0000', '24:00', '23:60', 'a00:00', '00:00a'):
widget = WcsExtraStringWidget('test', value='foo', required=False)
widget.field = fakefield
mock_form_submission(req, widget, {'test': invalid_case})
assert widget.has_error()
assert widget.error == 'Invalid time'
# and check it gets a special HTML input type
widget = WcsExtraStringWidget('test', value='foo', required=False)
widget.field = fakefield
assert 'type="time"' in str(widget.render())
def test_wcsextrastringwidget_django_validation():
class FakeField:
pass

View File

@ -1183,6 +1183,15 @@ class ValidationWidget(CompositeWidget):
'normalize_function': lambda v: v.upper().strip().replace(' ', ''),
},
),
(
'time',
{
'title': _('Time'),
'regex': r'([01]?[0-9]|2[0-3]):[0-5][0-9]',
'error_message': _('Invalid time'),
'html_input_type': 'time',
},
),
('regex', {'title': _('Regular Expression')}),
('django', {'title': _('Django Condition')}),
]