Thymeleaf不显示错误

时间:2018-07-30 05:08:33

标签: java spring spring-mvc thymeleaf

我有实体cliente,接触和方向,并且我想对每个属性进行验证。但是,当我想显示cliente的属性类(例如contacto)的错误时,百里香叶不进行验证也不显示错误。我不知道我在某些方面做错了吗,有人可以帮我吗?

@PostMapping("/form")
    public String guardar(@Valid Cliente cliente, BindingResult result, SessionStatus status, Model model) {

        if (result.hasErrors()) {
            model.addAttribute("titulo", "Crear Cliente");
            return "form";
        }

        if(cliente == null){
            model.addAttribute("titulo","Crear Cliente");
            return "form";
        }

        clienteService.save(cliente);
        status.setComplete();

        return "redirect:/listar";
   }

模板:

<div class="container py-4">
<form th:action="@{/form}" method="post" th:object="${cliente}">
    <div class="card border-dark my-2">
        <div class="card-header">Cliente</div>
        <div class="card-body">
            <div th:object="${cliente}" th:remove="tag">
                <ul th:if="${#fields.hasErrors('*')}" class="alert alert-danger" role="alert">
                    <li th:each="err: ${#fields.errors('*')}" th:text="${err}"></li>
                </ul>
            </div>
            <div class="form-row">
                <div class="form-group col-md-4">
                    <label>Raz&oacute;n Social </label>
                    <input type="text" class="form-control" th:field="*{razonSocial}" required="required">
                    <small th:if="${#fields.hasErrors('razonSocial')}" th:errors="*{razonSocial}"
                           class="form-text text-danger"></small>
                </div>
                <div class="form-group col-md-4">
                    <label>RFC </label>
                    <input type="text" class="form-control" th:field="*{rfc}">
                    <small th:if="${#fields.hasErrors('rfc')}" th:errors="*{rfc}"
                           class="form-text text-danger"></small>
                </div>
                <div class="form-group col-md-4">
                    <label>Uso del CFDI</label>
                    <input type="text" class="form-control" th:field="*{usoCfdi}" required="required">
                    <small th:if="${#fields.hasErrors('usoCfdi')}" th:errors="*{usoCfdi}"
                           class="form-text text-danger"></small>
                </div>
            </div>
        </div>
    </div>
    <div class="card border-dark my-2">
        <div class="card-header">Contacto</div>
        <div class="card-body">
            <div class="form-row">
                <div class="form-group col-md-6">
                    <label>Email </label>
                    <input type="email" class="form-control" th:field="*{contacto.email}">
                    <small th:if="${#fields.hasErrors('contacto.email')}" th:errors="*{contacto.email}"
                           class="form-text text-danger"></small>
                </div>
                <div class="form-group col-md-3">
                    <label>Telefono </label>
                    <input type="number" class="form-control" th:field="*{contacto.telefono}">
                    <small th:if="${#fields.hasErrors('contacto.telefono')}" th:errors="*{contacto.telefono}"
                           class="form-text text-danger"></small>
                </div>
                <div class="form-group col-md-3">
                    <label>Telefono 2 </label>
                    <input type="number" class="form-control" th:field="*{contacto.telefono2}">
                    <small th:if="${#fields.hasErrors('contacto.telefono2')}" th:errors="*{contacto.telefono2}"
                           class="form-text text-danger"></small>
                </div>
            </div>
        </div>
    </div>
    <div class="card border-dark my-2">
        <div class="card-header">Direcci&oacute;n</div>
        <div class="card-body">
        </div>
        <br>
        <div class="card">
            <div class="card-footer">
                <input type="submit" class="btn btn-primary btn-xs">
            </div>
        </div>
</form>

0 个答案:

没有答案