I want to edit my row in a datatable, but I have a problem. I have a validator which checks whether there is a social security number in the database. When I add everything is ok. The problem occurs when I'm editing. Below is the code fragment from my edit page:
<h:form>
<div id="userPanel">
<p:panel id="panelUser" header="Edytuj administratora" >
<div id="panelImage">
<img src="./../../images/person4.png" alt="person" width="150px" height="130px"/>
</div>
<h:inputHidden value="#{userMB.user.id}" />
<h:panelGrid columns="3">
<p:outputLabel for="firstName" value="#{msg.firstName}"></p:outputLabel>
<p:inputText id="firstName" value="#{userMB.user.firstName}" label="#{msg.firstName}" required="true">
<f:validator validatorId="firstNameValidator" />
<p:ajax update="msgFristName" event="keyup" />
</p:inputText>
<p:message for="firstName" id="msgFristName"/>
<p:outputLabel for="lastName" value="#{msg.lastName}"></p:outputLabel>
<p:inputText id="lastName" value="#{userMB.user.lastName}" label="#{msg.lastName}" required="true">
<f:validator validatorId="lastNameValidator" />
<p:ajax update="msgLastName" event="keyup" />
</p:inputText>
<p:message for="lastName" id="msgLastName"/>
<p:outputLabel for="personalId" value="#{msg.personalId}"></p:outputLabel>
<p:inputText id="personalId" value="#{userMB.user.personalId}" label="#{msg.personalId}" required="true">
<f:validator binding="#{personalIdValidator}" />
<p:ajax update="msgPersonalId" event="keyup" />
</p:inputText>
<p:message for="personalId" id="msgPersonalId"/>
<p:outputLabel for="password" value="#{msg.password}"></p:outputLabel>
<p:inputText id="password" value="#{userMB.user.password}" label="#{msg.password}" required="true">
<f:validator validatorId="passwordValidator" />
<f:attribute name="confirmPassword" value="#{confirmPassword}" />
<p:ajax update="msgPassword" event="keyup" />
</p:inputText>
<p:message for="password" id="msgPassword"/>
<p:outputLabel for="confirmPassword" value="#{msg.confirmPassword}"></p:outputLabel>
<p:inputText id="confirmPassword" binding="#{confirmPassword}" label="#{msg.confirmPassword}" required="true">
<f:validator validatorId="passwordValidator" />
<f:attribute name="confirmPassword" value="#{confirmPassword}" />
<p:ajax update="msgConfirmPassword" event="keyup" />
</p:inputText>
<p:message for="confirmPassword" id="msgConfirmPassword"/>
</h:panelGrid>
<center><p:commandButton value="#{msg.edit}" action="#{userMB.editUser()}" ajax="false">
<f:param name="userRole" value="admin" />
<f:param name="active" value="true" />
</p:commandButton>
<p:commandButton value="#{msg.cancel}" action="#{userMB.cancel()}" ajax="false" immediate="true"/></center>
</p:panel>
</div>
</h:form>
When I want to edit a user I click the button:
<center><p:commandButton value="#{msg.edit}" action="#{userMB.editUser()}" ajax="false">
which calls the method editUser
:
public String editUser() {
FacesContext context = FacesContext.getCurrentInstance();
Map requestParameterMap = (Map) context.getExternalContext().getRequestParameterMap();
try {
String userRole = requestParameterMap.get("userRole").toString();
String active = requestParameterMap.get("active").toString();
Boolean act = Boolean.parseBoolean(active);
user.setRole(userRole);
user.setActive(act);
if ((user.getEmail() != null) && (userDao.findEmailExist(user.getEmail()))) {
sendErrorMessageToUser("Użytkownik z podanym adresem email istnieje w bazie");
return null;
} else if ((user.getPersonalId() != null) && (userDao.findPersonalIdExist(user.getPersonalId()))) {
sendErrorMessageToUser("Użytkownik z podanym numerem pesel istnieje w bazie");
return null;
} else if ((user.getPhone() != null) && (userDao.findPhoneExist(user.getPhone()))) {
sendErrorMessageToUser("Użytkownik z podanym numerem telefonu istnieje w bazie");
return null;
} else {
userDao.update(user);
}
} catch (EJBException e) {
sendErrorMessageToUser("Błąd edycji użytkownika w bazie");
return null;
}
sendInfoMessageToUser("Konto zedytowane");
return user.getRole() + "List";
}
Method find personalIdExist returns true when it finds the personalId given during the editing, if it is not found it returns false.
And this is my problem. When I am editing User
my edit page looks this:
Example:
firstName: Pablo
lastName: ABCD
personalId: 12345678901
password: zxcv
When I am editing my personalId everything is ok, but I can only edit firstName
, lastName
and password
not personalId
. PersonalId
may be the same. When I click edit I have a problem, because my method personalIdExist
returns true
if the id already exists and I can't update my user.
I need somewhere to save the initial value personalId this user to be able to edit it. But do not know how.