add custom error message as invalid value message

This commit is contained in:
Frédéric Péters 2023-04-04 10:39:51 +02:00
parent 4d6dd6399f
commit a4b235b333
1 changed files with 13 additions and 5 deletions

View File

@ -1422,7 +1422,6 @@ class WcsExtraStringWidget(StringWidget):
prefill = False
prefill_attributes = None
validation_function = None
validation_function_error_message = None
def add_media(self):
if self.prefill_attributes and 'geolocation' in self.prefill_attributes:
@ -1434,13 +1433,22 @@ class WcsExtraStringWidget(StringWidget):
self.inputmode = ValidationWidget.get_html_inputmode(self.field.validation)
return super().render_content()
def get_error_message_codes(self):
yield from super().get_error_message_codes()
yield 'invalid_value'
def get_invalid_value_message(self):
validation_function_error_message = None
if self.field and self.field.validation:
validation_function_error_message = ValidationWidget.get_validation_error_message(
self.field.validation
)
return validation_function_error_message or _('invalid value')
def _parse(self, request):
StringWidget._parse(self, request)
if self.field and self.field.validation and self.value is not None:
self.validation_function = ValidationWidget.get_validation_function(self.field.validation)
self.validation_function_error_message = ValidationWidget.get_validation_error_message(
self.field.validation
)
normalized_value = self.value
if self.field and self.value and self.field.validation:
@ -1448,7 +1456,7 @@ class WcsExtraStringWidget(StringWidget):
normalized_value = normalize(self.value)
if self.value and self.validation_function and not self.validation_function(normalized_value):
self.set_error(self.validation_function_error_message or _('invalid value'))
self.set_error(error_code='invalid_value')
if self.field and self.value and not self.error and self.field.validation:
self.value = normalized_value