I am setting up a form using JSF (I'm pretty new at this) and I am getting a Validation Error: Value is not valid
message on one of the fields. This field is actually a separate object (as I will show below) that has a custom converter.
Here is what I have (with non-relevant code removed):
I have a Citation
class:
@ManagedBean(name="citation")
public class Citation {
private int id;
private Status status;
// getters and setters
}
I also have a Status
class that you see referenced in the Citation
class:
@ManagedBean(name="status")
public class Status {
private int id;
private String name;
// getters and setters
public List<Status> getAllStatuses() {
Session session = HibernateUtil.getCurrentSession();
session.beginTransaction();
session.clear();
Query query = session.createQuery("from Status");
List<Status> statuses = query.list();
try {
session.getTransaction().commit();
} catch (HibernateException e) {
// TODO: handle exception
session.getTransaction().rollback();
}
return statuses;
}
@Override
public boolean equals(Object obj) {
if (obj == null) return false;
if (!(obj instanceof Status)) return false;
if (this.id == ((Status)obj).getId()) {
return true;
} else {
return false;
}
}
@Override
public int hashCode() {
return this.name.hashCode();
}
}
Then for my form, I have:
<h:selectOneMenu id="citation_status" value="#{citation.status}">
<f:selectItems value="#{status.allStatuses} var="s" itemValue="#{s.id}" itemLabel="#{s.name}" />
</h:selectOneMenu>
<h:message for="citation_status" />
Lastly, for my converter, I have:
@FacesConverter(forClass=Status.class)
public class StatusConverter implements Converter {
@Override
public Object getAsObject(FacesContext context, UIComponent component, String value) {
// uses Hibernate to get the Status object (using a breakpoint in Eclipse, I have verified that this works)
// I can post this code if needed, but just trying to keep it short :)
}
@Override
public String getAsString(FacesContext context, UIComponent component, Object value) {
return String.valueOf(((Status) value).getId());
}
}
Now when I get to my form and submit, I get the Validation Error
next to the Status. I'm pretty new at this and thanks to @BalusC, I'm this far along.
Any help is greatly appreciated.