Jandako
Jandako

Reputation: 11

Spring boot thymeleaf validation not working

Hello I am introducing in sping boot MVC with thymeleaf

I have wacthed a lot of examples but I cant find why is not working properly.

I am trying to do a form validation with two fields "nombre" and "direccion" but it always validate the model as correct, redirecting to "mensajeView".

I have tried adding @RequestBody after @Valid and didn`t works.

My model:

@Entity
public class Almacen {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "idAlmacen", updatable = false, nullable = false)
    private int idAlmacen;
    
    
    @NotEmpty(message = "La clave no puede estar en blanco")
    private String nombre;
    @NotEmpty(message = "Error")
    private String direccion;

    public int getIdAlmacen() {
        return idAlmacen;
    }

    public void setIdAlmacen(int idAlmacen) {
        this.idAlmacen = idAlmacen;
    }

    public String getNombre() {
        return nombre;
    }

    public void setNombre(String nombre) {
        this.nombre = nombre;
    }

    public String getDireccion() {
        return direccion;
    }

    public void setDireccion(String direccion) {
        this.direccion = direccion;
    }

}

My controller

    @PostMapping("/crearAlmacen")
    public String crearAlmacenPost(@Valid Almacen almacen, BindingResult result, Model model) {
        if (result.hasErrors()) {
            return "crearAlmacen";
        } else {            
            model.addAttribute("mensaje","Operacion completada con exito");
            return "mensajeView";
        }
    }

My view:

    <form action="#" th:object="${almacen}" th:action="@{/crearAlmacen}" method="post">
        <div class="container">
            <div class="row justify-content-center align-items-center minh-100">
                <div class="col-lg-4">
                    <h2>Nuevo almacen</h2>


                    <div class="form-group">
                        <label for="nombre">Nombre</label> 
                        <input id="nombre" th:field="*{nombre}" placeholder="Nombre" type="text" class="form-control">
                        <td th:if="${#fields.hasErrors('nombre')}" th:errors="*{nombre}">Error</td>
                    </div>

                    <div class="form-group">
                        <label for="direccion">Dirección</label> 
                        <input id="direccion" th:field="*{direccion}" placeholder="Direccion" type="text" class="form-control">
                        <td th:if="${#fields.hasErrors('direccion')}" th:errors="*{direccion}">Error</td>
                    </div>
                 <button class="btn btn-success" type="submit">Guardar</button>
                </div>
            </div>
        </div>
    </form>

Upvotes: 0

Views: 884

Answers (2)

Yevhen Hlushchenko
Yevhen Hlushchenko

Reputation: 1

org.hibernate.validator.constraints is deprecated. I have same problem and i add dependencies.

<!-- add to work validation -->
    <dependency>
        <groupId>javax.validation</groupId>
        <artifactId>validation-api</artifactId>
        <version>2.0.0.Final</version>
    </dependency>
    <dependency>
        <groupId>org.hibernate.validator</groupId>
        <artifactId>hibernate-validator</artifactId>
        <version>6.0.2.Final</version>
    </dependency>
    <dependency>
        <groupId>org.hibernate.validator</groupId>
        <artifactId>hibernate-validator-annotation-processor</artifactId>
        <version>6.0.2.Final</version>
    </dependency>
    <!-- end validation  -->

Upvotes: 0

Jandako
Jandako

Reputation: 11

Finally I have solved it. I was importing notempty tags from javax.validation.constraints. I changed it for org.hibernate.validator.constraints and works perfect

Upvotes: 1

Related Questions