Reputation: 2585
I am following this guide to use Spring Form Validation but I cannot get the form to validate in my own program. I follow everything exactly how it's presented in the guide it just doesn't work in my application.
I used Spring Tools Suite and downloaded the sample application to see if it actually works and it does. I cannot see what exactly is causing the form validation to occur in the sample application and not mine. I tried to slowly change the sample application by removing dependencies from the POM to get it to break but it continues to work..... Which library exactly is doing the form validation?
I've seen others suggest that I need a particular validator on my classpath but the sample guide application makes absolutely no mention of this and I don't see anything special in their POM.
Controller :
@Controller
public class CreateEventController {
@RequestMapping(value="/event/create", method=RequestMethod.GET)
public String showForm(CreateEvent createEvent) {
return "createEvent";
}
@RequestMapping(value="/event/create", method=RequestMethod.POST)
public String checkEventForm(@Valid CreateEvent createEvent, BindingResult bindingResult){
if(bindingResult.hasErrors()){
return "createEvent";
}
return "redirect:/";
}
}
Backing Bean :
import javax.validation.constraints.NotNull;
public class CreateEvent {
@NotNull
private String title;
private String description;
private String password;
private String confirm;
// public getters and setters
}
Form :
<form action="#" th:action="@{/event/create}" th:object="${createEvent}" method="post">
<table>
<tr>
<td>Title:</td>
<td><input type="text" th:field="*{title}" /></td>
<td th:if="${#fields.hasErrors('title')}" th:errors="*{title}">Title Error</td>
</tr>
...
<tr> for the rest of the fields
...
</form>
POM :
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<version>1.4.181</version>
</dependency>
</dependencies>
Upvotes: 1
Views: 3915
Reputation: 2549
as @hooknc suggest above. here is woring pom for validator
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<!-- javax -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>javax.validation</groupId>
<artifactId>validation-api</artifactId>
<version>1.1.0.Final</version>
</dependency>
<!-- Hibernate -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
<version>5.1.0.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator-annotation-processor</artifactId>
<version>4.1.0.Final</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>3.2.10.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>3.2.10.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>3.2.10.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>3.2.10.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>3.2.10.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-expression</artifactId>
<version>3.2.10.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>3.2.10.RELEASE</version>
</dependency>
</dependencies>
Upvotes: 1