Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow invalid value error message to be customised #832

Open
markhobson opened this issue Jan 30, 2024 · 0 comments
Open

Allow invalid value error message to be customised #832

markhobson opened this issue Jan 30, 2024 · 0 comments
Labels
enhancement New feature, or existing feature improvement

Comments

@markhobson
Copy link

When a basic field coerces a string form data value into a stronger type it can raise an error message. For example, IntegerField adds an error message Not a valid integer value. if the value cannot be parsed as an integer. It would be useful to be able to customise this error message but it is hardcoded into the field.

Actual Behavior

>>> import wtforms
>>> class F(wtforms.Form):
...     foo = wtforms.IntegerField()
>>> f = F(foo="bar")
>>> f.validate()
False
>>> f.errors
{'foo': ['Not a valid integer value.']}

Expected Behavior

>>> import wtforms
>>> class F(wtforms.Form):
...     foo = wtforms.IntegerField(message="Enter foo as a number")
>>> f = F(foo="bar")
>>> f.validate()
False
>>> f.errors
{'foo': ['Enter foo as a number']}

This blog post details how the error message can be customised using i18n, but that is rather laborious. Another workaround is to extend the field class:

class CustomMessageIntegerField(IntegerField):
    """
    An integer field that allows the error message for invalid values to be customised.
    """

    def __init__(
        self,
        label: str | None = None,
        validators: tuple[Callable[[BaseForm, CustomMessageIntegerField], object], ...] | list[Any] | None = None,
        message: str = "Not a valid integer value.",
        **kwargs: Any,
    ):
        super().__init__(label, validators, **kwargs)
        self.message = message

    def process_data(self, value: Any) -> None:
        if value is not None:
            self._check_valid(value)

        super().process_data(value)

    def process_formdata(self, valuelist: list[Any]) -> None:
        if valuelist:
            self._check_valid(valuelist[0])

        super().process_formdata(valuelist)

    def _check_valid(self, value: Any) -> None:
        try:
            int(value)
        except ValueError:
            raise ValueError(self.message)

Although this feels like a lot of work for something that could be provided by the library.

This issue is applicable for the following basic fields:

  • IntegerField
  • DecimalField
  • FloatField
  • DateTimeField
  • DateField
  • TimeField
  • WeekField

Environment

  • Python version: 3.12.1
  • wtforms version: 3.1.2
@azmeuk azmeuk added the enhancement New feature, or existing feature improvement label Oct 11, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature, or existing feature improvement
Development

No branches or pull requests

2 participants