java - Edit row and validate -


i want edit row in datatable, have problem. have validator checks whether there social security number in database. when add ok. problem occurs when i'm editing. below code fragment 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 want edit user click button:

<center><p:commandbutton value="#{msg.edit}" action="#{usermb.edituser()}" ajax="false"> 

which calls 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 finds personalid given during editing, if not found returns false.

and problem. when editing user edit page looks this: example:

firstname: pablo

lastname: abcd

personalid: 12345678901

password: zxcv

when editing personalid ok, can edit firstname, lastname , password not personalid. personalid may same. when click edit have problem, because method personalidexist returns true if id exists , can't update user. need somewhere save initial value personalid user able edit it. not know how.

this interpretation of problem.

when edit user changing personalid error

sjuan76 approach correct one. don't want make validator check you. instead, focus on making sure user enters value personlid expect. then, make persistence layer enforce uniqueness (there's no need write own logic). i'm assuming using orm (but i'm not sure one) usermb should (pseudo code i'm writing off top of head).

public class usermb {      private long personalid; //not sure actual type       @column(unique = true)//uniqueness constraint     public long getpersonalid() {          return personalid;      }      //remainder omitted } 

if using plain sql or variant make sure declare uniqueness constraint personalid column when create table. in both cases, when try update usermb personalid , else has exception thrown. catch , return error message.

with change no longer need following method in codition

findpersonalidexist(user.getpersonalid()) 

(which way root cause of problem) what's going happen if try update firstname, lastname without changing personalid run smoothly. if try edit personal id , else has update not allowed.

comment

this unrelated concrete problem i'm little concerned logic. have user id below (which i'm guessing 1 orm managing).

<h:inputhidden value="#{usermb.user.id}" /> 

therefore should not try find if column (e.g. phonenumber) exist passing value argument. should grab user instance directly based on unique id db , verify is not re entering same information. might run problems. here's scenario:

usera wants change phone number 123 345

userb has phone number 345

your code return true though user not entering same value (unless that's want). if want have unique values fields add unique="true" needed. no need these checks.

you can eliminate null checks supplying own converter or setting parameters in faces-config submitted empty strings interpreted null. please see link below

h:inputtext bound string property submitting empty string instead of null


Comments

Popular posts from this blog

Ansible - ERROR! the field 'hosts' is required but was not set -

customize file_field button ruby on rails -

SoapUI on windows 10 - high DPI/4K scaling issue -