Reputation: 401
I have this code:
<p:inplace id="staffno" editor="true">
<p:ajax event="save" listener="#{staffbean.onSave}" update="staffnoInplace messages" />
<p:inputText id="staffnoInplace" value="#{staffbean.vieweditStaff.staffno}"/>
</p:inplace>
In staffbean.onSave() I store this through JPA/Hibernate. In the case where its successful, I face no issues. It's stored in the DB.
My problem has to do with the cases where Hibernate throws an exception due to DB constraints. In those cases, I would like to have the inplace to remain in editable state and NOT change the value. Currently, it fails on DB level but I still see the value changed on the UI object to the incorrect value.
Upvotes: 2
Views: 723
Reputation: 10463
You should include a validation method or validator for the inputText
component so as to reverse the bean value and prevent execution of the onSave
event when the given ID already exists in the database.
Knowingly attempting to insert a record into a database hoping for database constraints to provide validation is a nebulous data security practice. I am not saying that database constraints are bad, I am just saying that you should make every attempt to validate and santize your inputs before attempting to insert into the database.
Include a validation method in your managed bean like such:
public void validateStaffNoId(FacesContext context, UIComponent component, Object value)
throws ValidatorException {
//Query DB to see if ID exists...
if (idExists()) {
//Throw validator exception
}
}
Then in your inputText component simply add the attribute:
<p:inputText ... validator="#{staffBean.validateStaffNoId}" ... />
Upvotes: 2