Here I have attached my project codes. I am trying to create a crude operation using input form. I have add a javascript code to add additional forms. Suppose, when I click Add Dependent button in formpage.jsp, it will add a similar form except for "bearer" field, because, "bearer" name will be same for all dependent. This example is for a family file submission. A family can have 2, 3, 4 or more members. I hope you understand my concept. As far I know, @Valid ApplicationForm applicationForm can handle single form. So, how can I add validation in controller class for multiple forms using @Valid ApplicationForm applicationForm ? Because the jsp form will be list of family member info.
Here are the codes of jsp, JpaRepository, controller class and model class
formpage.jsp
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<html>
<head>
<title>Form submission</title>
</head>
<body>
<div>
<form:form modelAttribute="applicationForm" method="post">
<form:input type="hidden" path="id" />
<form:input type="text" path="bearer" />
<form:input type="text" path="name" />
<form:input type="text" path="description" />
<form:errors path="description" />
<br/>
<button type="button" onclick="addDependent()">Add Dependent</button>
<input type="submit" value="Save" />
</form:form>
</div>
<script type="text/javascript">
function addDependent() {
var dependentsDiv = document.getElementById('dependents');
var index = dependentsDiv.children.length;
var newDependent = `
<div class="dependent">
<form:hidden path="dependents[${index}].id" />
<form:label path="dependents[${index}].name">Name:</form:label>
<form:input path="dependents[${index}].name" />
<br/>
<form:label path="dependents[${index}].description">Description:</form:label>
<form:input path="dependents[${index}].description" />
<br/>
</div>
`;
dependentsDiv.innerHTML += newDependent;
}
</script>
</body>
</html>
Model class ApplicationForm.java
@Entity
public class ApplicationForm {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;
private String name;
@Size(min = 5, message = "minimum 5 characters")
private String description;
private String bearer;
public ApplicationForm(int id, String name, String description, String bearer) {
this.id = id;
this.name = name;
this.description = description;
this.bearer = bearer;
}
public ApplicationForm() {
}
// Getters and setters
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getBearer() {
return bearer;
}
public void setBearer(String bearer) {
this.bearer = bearer;
}
@Override
public String toString() {
return "Id= " + id + ", Name= " + name + ", Description= " + description + ", Bearer= " + bearer;
}
}
Repository interface FormRepository.java
public interface FormRepository extends JpaRepository<ApplicationForm, Integer> {
}
Controller class HomeController.java
@Controller
public class HomeController {
@Autowired
private FormRepository repo;
@GetMapping("form")
public String formPage(ModelMap modelMap) {
ApplicationForm appForm = new ApplicationForm(0, "user", "");
modelMap.put("applicationForm", appForm);
return "formpage";
}
@PostMapping("form")
public String saveForm(@Valid ApplicationForm applicationForm, BindingResult result, ModelMap modelMap) {
if (result.hasErrors()) {
return "formpage";
}
repo.save(applicationForm);
return "redirect:/homepage";
}
}
I want to add validation for multiple forms while @Valid ApplicationForm applicationForm handle single form.
Dependentsthat you can just validate.