Spring Boot Validation BindingResult always contains no errors

Im creating my spring boot application. In my class User there are a lot of validation annotations. Also, im hadling validation issues in my controller using bindingResult.hasErrors().
The problem is that my binding result is always has no errors.

Here is my controller and User class:

@PostMapping("/registration")
    public String registerUser(Model model, @Valid User user, @ModelAttribute("confirmPassword") String confirmPassword, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            model.addAttribute("allProjects", projectService.getAllProjects());
            return "registration";
        }

        User existingUser = userService.findByLogin(user.getLogin());
        if (existingUser != null) {
            model.addAttribute("userExistsError", true);
            model.addAttribute("allProjects", projectService.getAllProjects());
            return "registration";
        }

        if (!user.getPassword().equals(confirmPassword)) {
            model.addAttribute("passwordMismatch", true);
            model.addAttribute("allProjects", projectService.getAllProjects());
            return "registration";
        }

        userService.saveUser(user);
        return "redirect:/home";
    }
import javax.validation.constraints.Email;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.Pattern;

@Entity
@Table(name = "users")
@Data
@NoArgsConstructor
@Builder
@AllArgsConstructor
public class User {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id")
    private int id;

    @NotBlank(message = "Login cannot be blank!")
    @Email(message = "Invalid email format")
    @Column(name = "login", length = 50, unique = true, nullable = false)
    private String login;

    @NotBlank(message = "Password cannot be blank!")
    @Pattern(regexp = "^(?=.*[0-9])(?=.*[a-z])(?=.*[A-Z])(?=.*[@#$%^&+=])(?=\\S+$).{8,}$",
            message = "Password must be at least 8 characters long, contain at least one digit, one lowercase letter, one uppercase letter, and one special character")
    @Column(name = "password", nullable = false)
    private String password;

    @Column(name = "created_at", updatable = false, columnDefinition = "TIMESTAMP DEFAULT CURRENT_TIMESTAMP")
    private java.sql.Timestamp createdAt;

    @Column(name = "roles", length = 20, nullable = false)
    private String roles;

    @ManyToOne
    @JoinColumn(name = "project_id")
    private Project project;
}

I`ve tried changing versions of validators and debugging my app, but nothing helped.

  • what kind of dependencies are you including? take a look at spring-boot-starter-validation if it’s missing maybe that could have the issue.

    – 

  • if it’s not a dependency issue, take a look at this answer: stackoverflow.com/a/69692047/3693843 it might shed some light.

    – 

  • Here are my dependencies: <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>3.2.0</version> <relativePath/> </parent> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-validation</artifactId> </dependency> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-validator</artifactId> <version>6.0.12.Final</version> </dependency>

    – 

Leave a Comment