0

I have a custom validator in a Flask-WTF form that looks like this:

def is_some_condition_true(arg: str) -> bool:
    return arg == "example"

def validation_func(self, arg: str) -> None:
    if not is_some_condition_true(arg):
        raise ValidationError("The <strong>validation</strong> failed")

As you can see, I'm trying to insert html in the error message. Unfortunately, the result looks like this:

enter image description here

How can I put html in the validation error message without it being escaped?

965311532
  • 506
  • 3
  • 14
  • 2
    Provided you use Jinja templating, it is simple to do this using Jinja's [safe](https://stackoverflow.com/q/3206344/15368978) filter. Wherever you display your error message, simply add `|safe`, as explained in the linked post. – Patrick Yoder Jun 18 '22 at 10:18

1 Answers1

1

I ended up going with the second answer from the question kindly pointed out by @Patrick Yoder, as I couldn't quite get the |safe trick to work and it just feels a bit tidier this way.

This is how my code looks like now:

from markupsafe import Markup

def is_some_condition_true(arg: str) -> bool:
    return arg == "example"

def validation_func(self, arg: str) -> None:
    if not is_some_condition_true(arg):
        error_msg = Markup("The <strong>validation</strong> failed")
        raise ValidationError(error_msg)
965311532
  • 506
  • 3
  • 14