th:errorclass not showing up

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

th:errorclass not showing up

shruggie
I'm having problems getting a Spring Boot 1.5.7 app with Thymeleaf 3.0.9 to show th:errorclass or even ${#fields.hasErrors()} to work.
bindingResults show the errors in the controllers, and returns the view correctly, but no error messages or classes are shown in the view.
Controller:
@PostMapping(value = "/data/{dataId}")
    public String post(@PathVariable("dataId") String dataId, @Valid @ModelAttribute("dataForm") DataForm dataForm, BindingResult bindingResult, Model model) {
        if (bindingResult.hasErrors()) {
            model = populateModel(model, dataId);
            model.addAttribute("bindingResult", bindingResult);
            model.addAttribute("dataForm", dataForm);
            return "view";
Html:
    <form id="dataForm" th:action="@{/data/{dataId}(dataId=${dataId})}" th:object="${dataForm}"
        <div id="errors" class="alert alert-error">
            <ul th:if="${#fields.hasErrors('*')}">
                <li th:each="err : ${#fields.errors('*')}" th:text="${err}"></li>
            </ul>
        </div>
<div id="fieldCol" class="col-sm-4">
                <div class="form-group" th:classappend="${#fields.hasErrors('field')} ? 'is-invalid'">
                    <label th:for="field" th:text="#{view.field}"></label>
                    <div>
                        <input type="text" th:field="*{field}" class="form-control" th:errorclass="form-control-danger"
                               readonly/>
                        <div th:if="${#fields.hasErrors('field')}" class="invalid-feedback"
                             th:text="#{view.fieldError}"></div>
                    </div>
                </div>
            </div>
Forgive the somewhat incomplete HTML, I tried to include everything valid, but since I have to rewrite everything to remove a lot of stuff I can't show, I didn't want to include unnecessary details.
Reply | Threaded
Open this post in threaded view
|

Re: th:errorclass not showing up

shruggie
Found the error.
The form object was added to the model twice, once as a new form. That must have confused things.