lundi 13 octobre 2014

Spring MVC Form Validation Does Not Work


Vote count:

0




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>


asked 3 mins ago







Spring MVC Form Validation Does Not Work

Aucun commentaire:

Enregistrer un commentaire