5 Replies Latest reply on Oct 16, 2012 11:07 PM by Siva P

    Input text fail to validate

    K Jav Newbie

      Hi all, I've been following closely to the codes from dataTableEdit.xhtml in RichFaces Showcase. TIll now, I had successfully implemented a datatable to display a list of user from database which include filtering and sorting. Im currently working on editing user row which I get the current user id when I click on the row and show the pop out panel. The panel allows the particular user information to be edited. I wanted to implement javax validation but it fails to show the error message when I input a invalid string. Im not sure the way I use param value to pass user id and assign to currentUserid is correct (highlighted in red). Here's part of my code:

       

      My table.xhtml

       

       

      <rich:column>

      <f:facet name="header">Action</f:facet>

        <a4j:commandLink execute="@this" render="@none" oncomplete="#{rich:component('confirmPane')}.show()">

           <h:graphicImage value="/resources/images/common/user_delete.png" />

           <a4j:param value="#{user.id}" assignTo="#{userUpdateBean.currentUserId}" />

      </a4j:commandLink>

      <a4j:commandLink render="editGrid" execute="@this" oncomplete="#{rich:component('editPane')}.show()">

           <h:graphicImage value="/resources/images/common/user_edit.png" />

           <a4j:param value="#{user.id}" assignTo="#{userUpdateBean.currentUserId}" />

      </a4j:commandLink>

      </rich:column>

       

       

      <rich:popupPanel header="Edit User Details" id="editPane" domElementAttachment="parent" width="400" height="170">

      <h:panelGrid columns="3" id="editGrid">

             

           <h:outputText value="Username" />   

           <h:inputText value="#{userUpdateBean.selectedUser.username}" id="userName">

                <rich:validator/>

           </h:inputText>

           <rich:message for="userName" />

                    

           <h:outputText value="Email" />   

           <h:inputText value="#{userUpdateBean.selectedUser.email}" id="userEmail">

                <rich:validator/>

           </h:inputText>

           <rich:message for="userEmail" />

           <h:panelGroup/>

      </h:panelGrid>

             

      <a4j:commandButton value="Store" render="table" execute="editPane"

      oncomplete="if (#{facesContext.maximumSeverity==null}) {#{rich:component('editPane')}.hide();}" action="#{userManagedBean.store}"/>

      <a4j:commandButton value="Cancel" onclick="#{rich:component('editPane')}.hide(); return false;" />

      </rich:popupPanel>

       

      My user class

       

      @Pattern(regexp = "^[\\w\\-]([\\.\\w])+[\\w]+@([\\w\\-]+\\.)+[a-zA-Z]{2,4}$" , message="Invalid email address")

      public String getEmail() {

      return email;

      }

       

      @Size(min=3, max=40)

      @Pattern(regexp="^[a-zA-Z0-9]+$")

      @NotBlank

      @Column(unique = true)

      @Field

      public String getUsername() {

      return username;

      }

        • 1. Re: Input text fail to validate
          iabughosh Master

          hello Jav,

          try rich:popupPanel domElementAttachment="form" and surround your "editGrid" with f:validateBean tag :

          <f:validateBean>

          <h:panelGrid columns="3" id="editGrid">

          </h:panelGrid>

          </f:validateBean>

           

          regards.

          • 2. Re: Input text fail to validate
            K Jav Newbie

            hey Ibrahim, I had tried ur solution but it still giving me javax.el.PropertyNotFoundException: /WEB-INF/flows/user/search.xhtml @107,92 value="#{userUpdateBean.selectedUser.username}": Target Unreachable, 'selectedUser' returned null
            From this error, I think is not the validation that is causing the error.

             

            From my previous codes, my command link parameter used to be
            <a4j:commandLink render="editGrid" execute="@this" >

            <rich:componentControl target="editPane" operation="show"/>

             

            Now I changed my codes to
            <a4j:commandLink render="editGrid" execute="@this" oncomplete="#{rich:component('editPane')}.show()">

             

            After this change, I got the above error and also my sorting/filtering not working due to the null error. Im wondering if the "oncomplete" is causing the error.  To my understanding, "oncomplete" attribute allows to invoke the JavaScript code right after the Ajax Response is returned back and the DOM tree of the browser is updated. So far when i click on the commandlink, the pop out shows and display the current user from that row, but it fails to validate and when I click the button store, it give the above error.

             

            Here's my userUpdatedBean code:

            @ManagedBean

            @RequestScoped

            public class UserUpdateBean {

             

            private int currentUserId;

            private User selectedUser;


            @ManagedProperty(value="#{userService}")

            private UserService userService;

             

             

            public User getSelectedUser() {

                                selectedUser = userService.findById((long) currentUserId);

                                return selectedUser;

                      }

             

            public void setSelectedUser(User selectedUser) {

                                this.selectedUser = selectedUser;

                      }


            public int getCurrentUserId() {

                                return currentUserId;

                      }

             

            public void setCurrentUserId(int currentUserId) {

                                this.currentUserId = currentUserId;

                      }


            public UserService getUserService() {

                                return userService;

                      }


            public void setUserService(UserService userService) {

                                this.userService = userService;

                      }

             

            }

             

            Edited: I just found out that the following code highlight in red causes error. Initially user did not click any row, therefore the currentUserId will be 0 which cause the null issue, but I put the code value="#{userUpdateBean.selectedUser.username}" when I click on any row, why does it execute even before I click on any row? Sorry..Im still new to Spring and RichFaces, any advise will be great!



            • 3. Re: Input text fail to validate
              Siva P Master

              Hi Jav,

               

              As per my understanding Now you are able to show the datatable in the screen with Column Header Action and values like Delete and Edit.

              When you click the Edit button it show popup panel with the Specific row values,

              ((the pop out shows and display the current user from that row, but it fails to validate and when I click the button store, it give the above error.)) Here are you changing any values for the Current user textbox and making it as Empty value???? Or just clicking the Store button in the popup screen???

               

              Basically when the page loads all the getter methods will be executed in the backend.Check that you are able to get the Current user id in the UserUpdateBean.java .

              Check userService.findById((long) currentUserId); is returning some value(selectedUser OBJECT) or not?If Possible share some screens..

              • 4. Re: Input text fail to validate
                K Jav Newbie

                Hi sivaprasad, I understand that now when the page loads, all getter methods are executed in the backend, therefore this code selectedUser = userService.findById((long) currentUserId); will generate null error as currentUserId is 0 ( row not click initially ). What I did was I set currentUserId initially as 1 so when the page get loaded at the start, the return value wont be null, then after when user click on any row, the set method will change the value of curretUserId:

                 

                private int currentUserId = 1;


                public User getSelectedUser() {

                  selectedUser = userService.findById((long) currentUserId);

                                    return selectedUser;

                }

                 

                But my approach is not gd enough. What if user delete user id = 1, then getSelectedUser will return null again. Is there any gd approach to this problem? How can I avoid the null problem cause by currentUserId at the start when all getter methods are executed?

                 

                Here are some screenshots:

                Display list of users in datatable from mysql:

                displayuser.png

                 

                Click on row, popout, if user edit textbox, will validate:
                edituser.png

                • 5. Re: Input text fail to validate
                  Siva P Master

                  Hi Jav,

                   

                  By defauly in all the applicatiion development there is some default users for testing and admin check up.

                  For Example:test USER,

                  If possible  make load this user while loading the page ,so then you will not get any null error and alsoHide that user row in the datatable screen.Also try some thing like in backend keeping ACTIVE :Y/N for all the users.

                  OR

                  When ever the user logs in put the logged in user id in the session and take it from that and set in the getter methjod by default.In this case there is no null at all.

                   

                  Thanks,

                  Siva