I want to apply an additional is-invalid CSS class to a wtforms element only if there is an error present in the form.
I've landed on the following code to achieve this and it works:
{% if form.email.errors %}
{{ form.email(placeholder="Email", class="form-control is-invalid") }}
{% else %}
{{ form.email(placeholder="Email", class="form-control") }}
{% endif %}
However, it's not very consise and i'm having to repease the whole form.email element. This could get real messy if the logic was more complicated.
There must be a cleaner way to have only the class value wrapped in logic.
You could use the string concatenation operator "~" with an inline if statement, e.g.