misc: add a max length option to string fields (#40440) #774
|
@ -906,6 +906,19 @@ def test_wcsextrastringwidget_builtin_validation():
|
||||||
assert widget.error == 'You should enter a 5-digits zip code, for example 75014.'
|
assert widget.error == 'You should enter a 5-digits zip code, for example 75014.'
|
||||||
|
|
||||||
|
|
||||||
|
def test_wcsextrastringwidget_maxlength():
|
||||||
|
widget = WcsExtraStringWidget('test', value='foo', required=False, maxlength=10)
|
||||||
|
mock_form_submission(req, widget, {'test': '123'})
|
||||||
|
assert not widget.has_error()
|
||||||
|
|
||||||
|
widget = WcsExtraStringWidget('test', value='foo', required=False, maxlength=10)
|
||||||
|
mock_form_submission(req, widget, {'test': '1234567890abcdef'})
|
||||||
|
assert widget.has_error()
|
||||||
|
|
||||||
|
form = MockHtmlForm(widget)
|
||||||
|
assert 'maxlength="10"' in form.as_html
|
||||||
|
|
||||||
|
|
||||||
def test_wcsextrastringwidget_phone():
|
def test_wcsextrastringwidget_phone():
|
||||||
class FakeField:
|
class FakeField:
|
||||||
pass
|
pass
|
||||||
|
|
|
@ -45,7 +45,8 @@ class StringField(WidgetField):
|
||||||
|
|
||||||
widget_class = WcsExtraStringWidget
|
widget_class = WcsExtraStringWidget
|
||||||
size = None
|
size = None
|
||||||
extra_attributes = ['size']
|
maxlength = None
|
||||||
|
extra_attributes = ['size', 'maxlength']
|
||||||
validation = {}
|
validation = {}
|
||||||
data_source = {}
|
data_source = {}
|
||||||
keep_raw_value = False
|
keep_raw_value = False
|
||||||
|
@ -85,6 +86,13 @@ class StringField(WidgetField):
|
||||||
value=self.validation,
|
value=self.validation,
|
||||||
advanced=True,
|
advanced=True,
|
||||||
)
|
)
|
||||||
|
form.add(
|
||||||
|
StringWidget,
|
||||||
|
'maxlength',
|
||||||
|
title=_('Maximum number of characters'),
|
||||||
|
value=self.maxlength,
|
||||||
|
advanced=True,
|
||||||
|
)
|
||||||
form.add(
|
form.add(
|
||||||
data_sources.DataSourceSelectionWidget,
|
data_sources.DataSourceSelectionWidget,
|
||||||
'data_source',
|
'data_source',
|
||||||
|
@ -97,7 +105,13 @@ class StringField(WidgetField):
|
||||||
)
|
)
|
||||||
|
|
||||||
def get_admin_attributes(self):
|
def get_admin_attributes(self):
|
||||||
return WidgetField.get_admin_attributes(self) + ['size', 'validation', 'data_source', 'anonymise']
|
return WidgetField.get_admin_attributes(self) + [
|
||||||
|
'size',
|
||||||
|
'validation',
|
||||||
|
'data_source',
|
||||||
|
'anonymise',
|
||||||
|
'maxlength',
|
||||||
|
]
|
||||||
|
|
||||||
def get_view_value(self, value, **kwargs):
|
def get_view_value(self, value, **kwargs):
|
||||||
value = value or ''
|
value = value or ''
|
||||||
|
|
|
@ -646,7 +646,7 @@ class StringWidget(QuixoteStringWidget):
|
||||||
QuixoteStringWidget._parse(self, request)
|
QuixoteStringWidget._parse(self, request)
|
||||||
if self.value:
|
if self.value:
|
||||||
self.value = self.value.strip()
|
self.value = self.value.strip()
|
||||||
if self.maxlength and len(self.value) > self.maxlength:
|
if self.maxlength and len(self.value) > int(self.maxlength):
|
||||||
self.set_error_code('too_long')
|
self.set_error_code('too_long')
|
||||||
elif self.validation_function:
|
elif self.validation_function:
|
||||||
try:
|
try:
|
||||||
|
@ -655,7 +655,7 @@ class StringWidget(QuixoteStringWidget):
|
||||||
self.set_error(str(e))
|
self.set_error(str(e))
|
||||||
|
|
||||||
def get_too_long_message(self):
|
def get_too_long_message(self):
|
||||||
return _('Too long, value must be at most %d characters.') % self.maxlength
|
return _('Too long, value must be at most %d characters.') % int(self.maxlength)
|
||||||
|
|
||||||
def get_error_message_codes(self):
|
def get_error_message_codes(self):
|
||||||
yield from super().get_error_message_codes()
|
yield from super().get_error_message_codes()
|
||||||
|
|
Loading…
Reference in New Issue