2 Replies Latest reply on Jul 28, 2009 7:20 AM by Ambrish Singh

    On Validation Failure Form values are dissappearing for some fields.

    Ambrish Singh Newbie

      Hi Everyone,


      I have a form on which have fields like Firstname , Lastname, Username, Password, Confirm Password ,Email which are related with an Entity User. Besides these fields my forms also contains some Metadata information for each User.


      I have written two validators, one for Password and another one for Email. Now whenever the email validation failed,and the form is reRendered then the values which are entered for Metadata information are dissappears while fields which are related with User entity remains filled.


      The code for accepting Metadata values is :




      <rich:dataGrid var="i" value="#{manageUser.userMetaDataLength}"
                                              columns="2" styleClass="dataGrid_meta">
                                              <s:decorate template="../layout/edit.xhtml" id="metaDataDetails" >
                                                   <ui:define name="label">#{manageUser.userMetaDataList.get(i).strLabel}</ui:define>
                                                   <h:inputText id="intMetadata" 
                                                        value="#{manageUser.userExtentionList.get(i).strValue}" 
                                                        validator="#{manageUser.validateNumeric}"                                                  
                                                        required="#{manageUser.userMetaDataList.get(i).flgMandatory == 1}" 
                                                        requiredMessage="Value is required"
                                                        rendered="#{manageUser.userMetaDataList.get(i).strDataType == 'integer'}" />
                                                   <h:inputText id="charMetadata" 
                                                        value="#{manageUser.userExtentionList.get(i).strValue}" 
                                                        required="#{manageUser.userMetaDataList.get(i).flgMandatory == 1}" 
                                                        requiredMessage="Value is required"                                                   
                                                        rendered="#{manageUser.userMetaDataList.get(i).strDataType == 'varchar'}" />
                                                   <h:inputTextarea id="textMetadata" 
                                                        value="#{manageUser.userExtentionList.get(i).strValue}"
                                                        rendered="#{manageUser.userMetaDataList.get(i).strDataType == 'text'}" 
                                                        required="#{manageUser.userMetaDataList.get(i).flgMandatory == 1}" 
                                                        requiredMessage="Value is required" 
                                                        
                                                        rows="8" styleClass="metaDataTextArea" />
                                              </s:decorate>
                                         </rich:dataGrid>





      Thanks


      Ambrish

        • 1. Re: On Validation Failure Form values are dissappearing for some fields.
          Ambrish Singh Newbie

          Hi Everyone,


          One more thing I want to add, when I click on Add User button, then at that time I am generating an empty list for accepting Metadata information. This is done as shown below:




          @SuppressWarnings("unchecked")
          public void loadMetaDataList() {
          
          userMetaDataList = entityManager.createQuery(                              "from UserMetaData umd where umd.flgDeleted='0'").getResultList();
          
          userMetaDataLength = new ArrayList<Integer>();
          userMetaDataLength = userMetaDataList.size();     
          
          loadExtentionList();
          
          
          }



          Now inside loadExtentionList() method ..




          userExtentionList = new ArrayList<UserExtention>();
          // when a user is added, meta-data is also added from the same
          // screen, for that, create a list of empty objects
          
          for (Integer i : userMetaDataList)
          // adding empty objects to the list
          userExtentionList.add(new UserExtention(new UserMetaData(),userMaster));



          and here is the code of my validators ,




          @Begin(join = true)
               public void validateUserEmail(FacesContext context, UIComponent toValidate, Object value) {
                    
                    String userEmail = (String) value;
                    String email_regex = "^[\\w\\.-]+@([\\w\\-]+\\.)+[A-Z]{2,6}$";
                    
                    Pattern pattern = Pattern.compile(email_regex,Pattern.CASE_INSENSITIVE);
                    Matcher matcher = pattern.matcher(value.toString());
                              
                    List<UserMaster> existing = (List<UserMaster>) entityManager.createQuery(
                    "from UserMaster uinfo where uinfo.flgDeleted='0' and uinfo.strEmail=:email")
                    .setParameter("email", userEmail)
                    .getResultList();
                    
                    if(!matcher.find()) {
                         ((UIInput)toValidate).setValid(false);
                         SeamResourceBundle srb = new SeamResourceBundle(); 
                         String invalidEmail = srb.getString("message.valid.user.email");
                         FacesMessage message = new FacesMessage(invalidEmail);
                         message.setSeverity(javax.faces.application.FacesMessage.SEVERITY_ERROR);               
                         context.addMessage(toValidate.getClientId(context), message);
                    }
                              
                    else if(existing.size() > 0 && !(loadListFromDataBase)){
                         ((UIInput)toValidate).setValid(false); 
                         SeamResourceBundle srb = new SeamResourceBundle();  
                         String duplicateEmail = srb.getString("message.duplicate.user.uemail");
                         FacesMessage message = new FacesMessage(duplicateEmail);
                         message.setSeverity(javax.faces.application.FacesMessage.SEVERITY_ERROR);               
                         context.addMessage(toValidate.getClientId(context), message);
                    }
               }
          
          



          Thanks


          Ambrish



          • 2. Re: On Validation Failure Form values are dissappearing for some fields.
            Ambrish Singh Newbie

            Hi Everyone,


            I have not got the solution yet.
            Here is the code where I am calling my validator, when the validation fails
            the filled metdata values for some fields are disappearing.




            <h:panelGrid columns="1" border="0"
                                                    columnClasses="formLabel,formValue" rowClasses="formLabel">
                                                    <s:decorate id="emailDecoration" template="../layout/edit.xhtml">
                                                         <ui:define name="label">Email</ui:define>
                                                         <h:inputText id="email" value="#{userMaster.strEmail}" required="true" 
                                                         requiredMessage="#{messages['message.required.user.email']}" 
                                                         validator="#{manageUser.validateUserEmail}" disabled="#{manageUser.disabled}"/> 
                                                    </s:decorate> 
            </h:panelGrid>



            Any help would be appreciated.


            Thanks


            Ambrish