Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Validation form in Spring using @Valid not work

I want to validation my form, but this not work. My entity class

import java.io.Serializable;
import java.util.Set;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;

import org.hibernate.validator.constraints.Email;

@Entity
@Table(name = "users")
public class User implements Serializable {

    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue
    @Column(name = "id")
    private Integer id;
    @NotNull
    @Size(max = 20)
    @Column(name = "username")
    private String username;
    @NotNull
    @Size(max = 20)
    @Column(name = "password")
    private String password;
    @NotNull
    @Size(max = 20)
    @Column(name = "firstName")
    private String firstName;
    @NotNull
    @Size(max = 20)
    @Column(name = "lastName")
    private String lastName;
    @Size(min = 11, max = 11)
    @Column(name = "personalId")
    private String personalId;
    @Size(max = 40)
    @Column(name = "city")
    private String city;
    @Size(max = 40)
    @Column(name = "address")
    private String address;
    @NotNull
    @Email
    @Size(max = 30)
    @Column(name = "email")
    private String email;
    @Size(min = 9, max = 9)
    @Column(name = "phone")
    private String phone;
    @OneToMany(mappedBy = "user")
    private Set<UserRole> userRoleSet;
}

adminList.jsp and form go to addAdmin page:

<form action="addAdminForm" method="post">
    <input type="submit" value="Dodaj administratora" />
</form>

addAdmin.jsp page formule:

    <form:form action="addAdmin" modelAttribute="user" method="post">
    <form:label path="username">Login: </form:label>
    <form:input path="username" />
    <form:errors path="username" cssClass="error" />
    <br />
    <form:label path="password">Hasło: </form:label>
    <form:password path="password" />
    <form:errors path="password" cssClass="error" />
    <br />
    <form:label path="firstName">Imię: </form:label>
    <form:input path="firstName" />
    <form:errors path="firstName" cssClass="error" />
    <br />
    <form:label path="lastName">Nazwisko: </form:label>
    <form:input path="lastName" />
    <form:errors path="lastName" cssClass="error" />
    <br />
    <form:label path="email">Email: </form:label>
    <form:input path="email" />
    <form:errors path="email" cssClass="error" />
    <br />
    <input type="submit" value="Dodaj" />
</form:form>

Controller:

    @RequestMapping(value = "/admin/addAdminForm", method = RequestMethod.POST)
public ModelAndView goAddAdminForm() {
    ModelAndView mav = new ModelAndView("admin/addadmin");
    mav.addObject("user", new User());
    return mav;
}

@RequestMapping(value = "/admin/addAdmin", method = RequestMethod.POST)
public String addAdmin(@Valid @ModelAttribute("user") User user,
        BindingResult result, Model model) {
    if (result.hasErrors()) {
        return "admin/addadmin";
    } else {
        userService.createUser(user);
        user = userService.findByUsername(user.getUsername());
        UserRole userRole = new UserRole("ROLE_ADMIN");
        userRole.setUser(user);
        userRoleService.createUserRole(userRole);
        return "redirect:/admin/adminlist";
    }
}

When i try send empty formule i should get error messages result.hasErrors() not return error and my application go to else and try save user. Why @Valid not work?

like image 564
The Nightmare Avatar asked Nov 24 '14 18:11

The Nightmare


2 Answers

After upgrading my project to Spring Boot 2.3.0, I struggled for hours with the same issue until I realized that as of #19550, Web and WebFlux starters do not depend on the validation starter by default anymore. If your application is using validation features, you’ll need to manually add back a dependency on spring-boot-starter-validation in your build file.

like image 134
ntholi Avatar answered Oct 20 '22 03:10

ntholi


I struggled with the same issue in my Spring Boot 2.4.1 project. You'll need to add this dependency in your pom.xml file

...
<dependency>
 <groupId>org.springframework.boot</groupId>
 <artifactId>spring-boot-starter-validation</artifactId>
</dependency>
...
like image 37
j.Frederic Avatar answered Oct 20 '22 04:10

j.Frederic