baymon
baymon

Reputation: 459

Thymeleaf errors messages optimization

I looked at this tutorial: https://spring.io/guides/gs/validating-form-input.

Using th:errors results in <br> separeted error messages. I want to have an unordered list. So I've defined a fragment like this ...

<td th:fragment="validationMessages(field)" th:if="${#fields != null and field != null and #fields.hasErrors(field)}">
    <ul>
        <li th:each="error : ${#fields.errors(field)}" th:text="${error}"></li>
    </ul>
</td>

... and using it with ...

<td th:replace ="form :: validationMessages('age')"></td>

Is there a "clean code" solution / best practice, like overriding the render implementation of th:errors?

Upvotes: 0

Views: 300

Answers (1)

user65839
user65839

Reputation:

You could probably create your own Thymeleaf Processor, based on org.thymeleaf.spring4.processor.attr.SpringErrorsAttrProcessor, that used your own method of delimiting errors, and then use that rather than the one Thymeleaf gives you. It doesn't look particularly designed for extending, though.

I think the way you did it is probably best. I tend to prefer my HTML templates to be in a templating language (like Thymeleaf) rather than in Java code. You can modify as needed (such as adding styling classes) and it's clear what the code does. This is exactly the kind of thing template fragments are made for.

Upvotes: 1

Related Questions