Source code for form.errors

from __future__ import annotations

from onegov.form.i18n import _


from typing import TYPE_CHECKING
if TYPE_CHECKING:
    from wtforms import Field


[docs] class FormError(Exception):
[docs] def __repr__(self) -> str: return self.__class__.__name__
[docs] class InvalidMimeType(FormError): pass
[docs] class UnableToComplete(FormError): pass
[docs] class FormParsingError(FormError):
[docs] message_template = _('The form could not be parsed.')
[docs] def field_message(self, field: Field) -> str: return field.gettext(self.message_template)
[docs] class _FormParsingErrorWithLineContext(FormParsingError): def __init__(self, line: int):
[docs] self.line = line
[docs] def field_message(self, field: Field) -> str: return super().field_message(field).format(line=self.line)
[docs] def __repr__(self) -> str: return f'{self.__class__.__name__}(line={self.line})'
[docs] class InvalidFormSyntax(_FormParsingErrorWithLineContext):
[docs] message_template = _('The syntax on line {line} is not valid.')
[docs] class InvalidIndentSyntax(_FormParsingErrorWithLineContext):
[docs] message_template = _( 'The indentation on line {line} is not valid. ' 'Please use a multiple of 4 spaces' )
[docs] class InvalidCommentIndentSyntax(_FormParsingErrorWithLineContext):
[docs] message_template = _( 'The indentation on line {line} is not valid. ' 'Comments must be indented to the same level as ' 'the field definition (`=`) they belong to.' )
[docs] class InvalidCommentLocationSyntax(_FormParsingErrorWithLineContext):
[docs] message_template = _( 'Incorrect placement of the field description on ' 'line {line}. The field description must be placed ' 'below the field definition (`=`) and with the same ' 'indentation.' )
[docs] class _FormParsingErrorWithLabelContext(FormParsingError): def __init__(self, label: str):
[docs] self.label = label
[docs] def field_message(self, field: Field) -> str: return super().field_message(field).format(label=self.label)
[docs] def __repr__(self) -> str: return f'{self.__class__.__name__}(label={self.label!r})'
[docs] class DuplicateLabelError(_FormParsingErrorWithLabelContext):
[docs] message_template = _("The field '{label}' exists more than once.")
[docs] class EmptyFieldsetError(_FormParsingErrorWithLabelContext):
[docs] message_template = _( "The '{label}' group is empty and will not be visible. Either remove " "the empty group or add fields to it." )
[docs] class FieldCompileError(_FormParsingErrorWithLabelContext):
[docs] message_template = _( "The field '{label}' has no type defined. " "For example use '___' for a text field." )
[docs] class MixedTypeError(_FormParsingErrorWithLabelContext):
[docs] message_template = _( "The field '{label}' cannot mix radio buttons and checkboxes." )