Validation Error: Value is not valid
techbrainless May 29, 2012 11:01 AMHi All
I am developing a small application in which the user can enter the student details which includes the course in which the user can select 1 or more subjects
. adding the student works fine but while editing am facing a problem("Validation Error: Value is not valid") only when i reselect the course and the subjects
update will works fine if i dont change the course ,
faces config
[code]
<managed-bean>
<managed-bean-name>updateStudentInfoBean</managed-bean-name>
<managed-bean-class>com.example.UpdateStudentInfoBean</managed-bean-class>
<managed-bean-scope>request</managed-bean-scope>
<managed-property>
<property-name>selectedStudentInfo</property-name>
<property-class>com.example.StudentInfo</property-class>
<value>#{selectedStudentInfo}</value>
</managed-property>
</managed-bean>
<managed-bean>
<managed-bean-name>selectedStudentInfo</managed-bean-name>
<managed-bean-class>com.example.StudentInfo</managed-bean-class>
<managed-bean-scope>request</managed-bean-scope>
</managed-bean>
[/code]
editStudentInfo.xhmtl
[code]
<h:form>
<a4j:keepAlive beanName="selectedStudentInfo" />
<table align="center">
<tr>
<td>Student first Name:</td>
<td><h:inputText id="studentFirstName"
value="#{selectedStudentInfo.studentFirstName}">
</h:inputText></td>
<td><h:message style="color:red;" for="studentFirstName" /></td>
</tr>
<tr>
<td>Course Details :</td>
<td><h:selectOneMenu
value="#{selectedStudentInfo.courseInfo.courseId}"
converter="javax.faces.Integer" onchange="submit()"
immediate="true"
valueChangeListener="#{updateStudentInfoBean.processValueChange}">
<f:selectItems value="#{updateStudentInfoBean.allCourses}" />
</h:selectOneMenu></td>
</tr>
<tr>
<td>Subject Details :</td>
<td><rich:pickList
value="#{updateStudentInfoBean.selectedSubjects}"
converter="javax.faces.Integer">
<f:selectItems value="#{updateStudentInfoBean.allSubjects}"
converter="javax.faces.Integer" />
</rich:pickList>
</td>
</tr>
</table>
<p align="center">
<h:commandButton value="Submit"
action="#{updateStudentInfoBean.update}" />
</p>
</h:form>
[/code]
StudentInfo.java
[code]
public class StudentInfo {
private Integer studentInfo;
private String firstName;
private String lastName;
private CourseInfo courseInfo;
public Integer getStudentInfo() {
return studentInfo;
}
public void setStudentInfo(Integer studentInfo) {
this.studentInfo = studentInfo;
}
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 CourseInfo getCourseInfo() {
return courseInfo;
}
public void setCourseInfo(CourseInfo courseInfo) {
this.courseInfo = courseInfo;
}
}
[/code]
CourseInfo.java
[code]
public class CourseInfo {
private Integer courseId;
private String courseName;
public Integer getCourseId() {
return courseId;
}
public void setCourseId(Integer courseId) {
this.courseId = courseId;
}
public String getCourseName() {
return courseName;
}
public void setCourseName(String courseName) {
this.courseName = courseName;
}
}
[/code]
SubjectInfo.java
[code]
public class SubjectInfo {
private Integer subjectId;
private String subjectName;
private CourseInfo courseInfo;
public Integer getSubjectId() {
return subjectId;
}
public void setSubjectId(Integer subjectId) {
this.subjectId = subjectId;
}
public String getSubjectName() {
return subjectName;
}
public void setSubjectName(String subjectName) {
this.subjectName = subjectName;
}
public CourseInfo getCourseInfo() {
return courseInfo;
}
public void setCourseInfo(CourseInfo courseInfo) {
this.courseInfo = courseInfo;
}
}
[/code]
UpdateStudentInfoBean
[code]
public class UpdateStudentInfoBean {
private Integer selectedCourseId;
private List<Integer> selectedSubjectsInfo = new ArrayList<Integer>();
private StudentInfo selectedStudentInfo;
public List<SelectItem> getAllCourses() {
List<CourseInfo> coursesInfo = new DatabaseQueries().getCourses();
List<SelectItem> items = new ArrayList<SelectItem>();
for (CourseInfo courseInfo : coursesInfo)
items.add(new SelectItem(courseInfo.getCourseId(), courseInfo
.getCourseName()));
return items;
}
public List<SelectItem> getAllSubjects() {
List<SubjectInfo> subjectsInfo = null;
List<SelectItem> items = new ArrayList<SelectItem>();
if (this.selectedStudentInfo != null) {
if (this.selectedStudentInfo.getCourseInfo() != null) {
this.selectedCourseId = this.selectedStudentInfo
.getCourseInfo().getCourseId();
}
}
CourseInfo courseInfo = new DatabaseQueries()
.getCourseInfo(this.selectedCourseId);
if (courseInfo == null)
return items;
subjectsInfo = new DatabaseQueries().getSubjectsInfo(courseInfo);
for (SubjectInfo subjectInfo : subjectsInfo){
items.add(new SelectItem(subjectInfo.getSubjectId(),
subjectInfo.getSubjectName()));
}
return items;
}
public List<Integer> getSelectedSubjects() {
List<Integer> currentSelectedSubjects = new ArrayList<Integer>();
if ((this.selectedStudentInfo != null)) {
if (this.selectedStudentInfo.getCourseInfo() != null) {
List<StudentSubjectMapping> studentSubjectMappings = new DatabaseQueries()
.getSubjects(this.selectedStudentInfo);
if (studentSubjectMappings!=null){
if (studentSubjectMappings.size() > 0) {
for (StudentSubjectMapping studentSubjectMapping : studentSubjectMappings) {
currentSelectedSubjects.add(studentSubjectMapping
.getSubjectInfo().getSubjectId());
}
}
}
}
}
if (currentSelectedSubjects.size() > 0)
return currentSelectedSubjects;
else{
selectedSubjectsInfo.clear();
return selectedSubjectsInfo;
}
}
public void setSelectedPrerequisites(List<Integer> selectedPrerequisites) {
this.selectedSubjectsInfo = selectedPrerequisites;
}
public void processValueChange(ValueChangeEvent arg0)
throws AbortProcessingException {
this.setSelectedCourseId(Integer.getInteger(arg0.getNewValue()
.toString()));
}
public Integer getSelectedCourseId() {
return selectedCourseId;
}
public void setSelectedCourseId(Integer selectedCourseId) {
this.selectedCourseId = selectedCourseId;
}
public List<Integer> getSelectedSubjectsInfo() {
return selectedSubjectsInfo;
}
public void setSelectedSubjectsInfo(List<Integer> selectedSubjectsInfo) {
this.selectedSubjectsInfo = selectedSubjectsInfo;
}
public StudentInfo getSelectedStudentInfo() {
return selectedStudentInfo;
}
public void setSelectedStudentInfo(StudentInfo selectedStudentInfo) {
this.selectedStudentInfo = selectedStudentInfo;
}
}
[/code]