The validation is not done, and I cannot find where the error is, even when I enter an empty field, it passes and no message is displayed, it's the same problem for the size.
the only validation that works is min and max for the "freePases" field which does not allow me to enter more than 10 digits but no message is displayed there too, and I would like the validation to make sure that the number is between 0 and 10, not containing between 0 and 10 digits like it does now.
I have used hibernate validator 7 and validation Api 2.0.1 and I use IntelliJ Idea.
I don't know what to do, thanks in advance for your answers.
package com.spring.demo;
import javax.validation.constraints.Max;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
public class Customer {
private String firstName;
@NotNull(message="is required")
@Size(min=1, message="is required")
private String lastName;
@Min(value=0, message="must be greater than or equal to zero")
@Max(value = 10, message="must be less than or equal to 10")
private int freePasses;
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public int getFreePasses() {
return freePasses;
}
public void setFreePasses(int freePasses) {
this.freePasses = freePasses;
}
}
package com.spring.demo;
import javax.validation.Valid;
import org.springframework.beans.propertyeditors.StringTrimmerEditor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
@RequestMapping("/customer")
public class CustomerController {
@InitBinder
public void initBinder(WebDataBinder dataBinder) {
StringTrimmerEditor stringTrimmerEditor = new StringTrimmerEditor(true);
dataBinder.registerCustomEditor(String.class, stringTrimmerEditor);
}
@RequestMapping("/showForm")
public String showForm(Model theModel) {
theModel.addAttribute("customer", new Customer());
return "customer-form";
}
@RequestMapping("/processForm")
public String processForm(
@Valid @ModelAttribute("customer") Customer theCustomer,
BindingResult theBindingResult) {
//seeing if we have white spaces and if they passes validation
System.out.println("Last name: |" + theCustomer.getLastName() + "|");
if (theBindingResult.hasErrors()) {
return "customer-form";
}
else {
return "customer-confirmation";
}
}
}
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
<html>
<head>
<title>Customer Registration Form</title>
<style>
.error {color:red}
</style>
</head>
<body>
<i>Fill out the form, Asterisk (*) means required</i>
<br><br>
<form:form action="processForm" modelAttribute="customer">
First name :<form:input path="firstName"/>
<br><br>
Last name (*): <form:input path="lastName"/>
<form:errors paths="lastName" cssClass="error"/>
<br><br>
Free Passes (*): <form:input path="freePasses"/>
<form:errors paths="freePasses" cssClass="error"/>
<br><br>
<input type="submit" value="Submit">
</form:form>
</body>
</html>
you have to add this below in the pom.xml
Bean Validation API 2.0.1:
<dependency>
<groupId>javax.validation</groupId>
<artifactId>validation-api</artifactId>
<version>2.0.1.Final</version>
</dependency>
Hibernate Validator 7.0.1 Final:
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
<version>7.0.1.Final</version>
</dependency>
Then you have to update the maven.
This post also helps you to find a more descriptive answer. @NotNull annotation is not working in Spring boot application
If you don't have a pom.xml,
Add jar files to your lib
folder. But exactly your newly added jars, you can see in external libraries folder. Using these links, you can download hibernate-validator.jar and validation-api-2.0.1.final.jar.
This post may help you to add these jar files to lib folder Correct way to add external jars (lib/*.jar) to an IntelliJ IDEA project