0

I don't get any validation errors with the last name and first name fields. I tried adding hibernate dependency to maven but it still doesn't work. It only go to the next page without any errors. How to solve this problem?

Customer class:

public class Customer {

    @NotNull(message = "is required")
    @Size(min = 3, max = 20, message = "is required")
    private String firstName;

    @NotNull(message = "is required")
    @Size(min = 3, max = 20, message = "is required")
    private String lastName;
...
}

Controller:

 @RequestMapping("/processForm")
    public String processForm(@ModelAttribute("customer") @Valid Customer customer, BindingResult bindingResult) {

        if (bindingResult.hasErrors()) {
            return "customer-form";
        }
        System.out.printf("First name: %s\n Last name: %s\n", 
        customer.getFirstName(), customer.getLastName());
        return "customer-confirmation";
    }

Form:

<form:form action="processForm" modelAttribute="customer" method="post">
    <p>First name (*): <form:input path="firstName"/></p>
    <form:errors path="firstName" cssClass="error"/>
    <p>Second name: <form:input path="lastName"/></p>
    <form:errors path="lastName" cssClass="error"/>
    <input type="submit" value="Submit"/>
</form:form>

Maven:

<!-- https://mvnrepository.com/artifact/org.hibernate.validator/hibernate-validator -->
        <dependency>
            <groupId>org.hibernate.validator</groupId>
            <artifactId>hibernate-validator</artifactId>
            <version>6.0.17.Final</version>
        </dependency>
  • 1
    Possible duplicate of [Spring MVC: How to perform validation?](https://stackoverflow.com/questions/12146298/spring-mvc-how-to-perform-validation) – K.Nicholas Jul 26 '19 at 15:09
  • I did everything that was in that answer, but nothing helped – Barradot Jul 26 '19 at 15:29

0 Answers0