Reputation: 4048
I am trying to accomplish the following: I have a form that a user will fill-out, outside of the form there are (Let's say Student and Teacher for example).
Depending on which radio box is selected, I want the form to contain different fields.
For example, when Student is selected, the user would have to fill out the fields first name, last name and course name. When Teacher is selected is selected, the user would have to fill first name, last name and year of experience.
As you can see, some of the fields are the same.
I am planning to use the same bean to manage the 2 "types" of users (Student and Teacher). I will just have a column "type" in my DB to make the difference.
What I do not know is, does my controller has anything to do with that or should I handle everything within my JSP with JQuery? I have no idea how to perform that and what is the best practice.
Also, depending on which value of the combo box has been selected, I would need to insert a different "type" value in the DB as I previously said, but how do I get the combo-box value from the JSP to the controller?
Here is my AddUserFormController right now:
@Controller
@RequestMapping("/register.htm")
public class AddUserFormController {
@Autowired
private UserDetailsService userDetailsService;
@Autowired
private UserEntityService userEntityService;
public void setUserDetailsService(UserDetailsService userDetailsService) {
this.userDetailsService = userDetailsService;
}
@RequestMapping(method = RequestMethod.POST)
public String onSubmit(@ModelAttribute("userEntity") UserEntity user, BindingResult result)
throws ServletException {
((UserEntityService) userEntityService).addUser(user);
return "/home";
}
@RequestMapping(method = RequestMethod.GET)
public String showAddForm(ModelMap model) {
UserEntity user = new UserEntity();
model.addAttribute(user);
return "/register";
}
protected Object formBackingObject(HttpServletRequest request) throws ServletException {
AddUser addUser = new AddUser();
return addUser;
}
public UserDetailsServiceImpl getUserDetailsService() {
return (UserDetailsServiceImpl) userDetailsService;
}
}
Here is my JSP (I simplified it, there is no username and password field here to make it easier in the post). There are 3 fields right now, first name, last name and course name (the fields that I want to display if the combo box that is selected is "Student"). I do not know what do add here and how to handle it so different fields are displayed depending on which combo box is selected:
<!DOCTYPE HTML>
<%@ include file="/WEB-INF/pages/include.jsp"%>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
<html>
<head>
<title>Test</title>
</head>
<head>
<title>Register</title>
</head>
<body>
<%@ include file="/WEB-INF/pages/menu.jsp"%>
<div class="container">
<form:form method="post" modelAttribute="userEntity">
<label class="radio"> <input type="radio"
name="optionsRadios" id="optionsRadios1" value="option1" checked>
I am a Teacher
</label>
<label class="radio"> <input type="radio"
name="optionsRadios" id="optionsRadios2" value="option2"> I
am a Student
</label>
<table width="95%" bgcolor="f8f8ff" border="0" cellspacing="0"
cellpadding="5">
<tr>
<td align="right" width="20%">First Name</td>
<td width="20%"><form:input path="firstName" /></td>
<td width="60%"><form:errors path="firstName" cssClass="error" /></td>
</tr>
<tr>
<td align="right" width="20%">Last Name</td>
<td width="20%"><form:input path="lastName" /></td>
<td width="60%"><form:errors path="lastName" cssClass="error" /></td>
</tr>
<tr>
<td align="right" width="20%">Course Name</td>
<td width="20%"><form:input path="courseName" /></td>
<td width="60%"><form:errors path="courseName"
cssClass="error" /></td>
</tr>
<button type="submit" class="btn btn-success">Register</button>
</form:form>
</div>
</body>
</html>
Here is my Bean that is also simplified (I did not put the username and password attributes here to simplify the post):
@Entity
@Table(name = "user", catalog = "test", schema = "")
public class UserEntity implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Basic(optional = false)
@Column(name = "id")
private Integer id;
@Column(name = "first_name")
private String firstName;
@Column(name = "family_name")
private String familyName;
@Column(name = "course_name")
private String course_name;
@Column(name = "experience")
private String experience;
public UserEntity() {
}
public UserEntity(Integer id) {
this.id = id;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getCourseName() {
return course_name;
}
public void setCourseName(String course_name) {
this.course_name = course_name;
}
public String getExperience() {
return experience;
}
public void setExperience(String experience) {
this.experience = experience;
}
}
Upvotes: 0
Views: 4359
Reputation: 1468
The solution here is using Jquery to show or hide the required fields according to the radio button that is selected.
Here is a JSFIDDLE that shows this implementation. The Jquery function that handles this is
function showSpecificFields(obj){
if($(obj).is(":checked")){
var radioVal = $(obj).val();
$(".fieldsSpecific").each(function(){
if($(this).attr('id') == radioVal){
$(this).show();
} else{
$(this).hide();
}
});
}
}
It basically checks the value of the selected radio button with the id
of the html
tag that contains the fields that are to be displayed.
I have showed this using simple html, but using tables for aligning form elements is not the best idea. But the Jquery logic can be handy even for different html tags.
Upvotes: 1