6 Replies Latest reply on Sep 19, 2009 8:55 AM by Nick Belaevski

    Server side validation

    Shaiju CK Newbie

      Hi,
      I am a newbi in JSF and richfaces.
      I am trying to do a business valiadtion(verifying the userName already existing) on the tab out of for the userName text box.
      I tried to use "rich:ajaxValidator" , but doesnt seems to be woring.
      my code is below.

      <h:inputText value="#{userBean.userName}" id="name" required="true">
       <f:validateLength minimum="3" maximum="12"/>
       <rich:ajaxValidator event="onblur" ajaxListener="#{userBean.checkName}" />
      
       </h:inputText>
      

      I wanted to do the validation logic inside checkName method.i am not sure this is the right approach.Can anybody suggest a solution ?

      Thanks in advance
      Shaiju

        • 1. Re: Server side validation
          gopi b Newbie

          Hi Shaiju, I am giving to example how to check the server side validation in username will already exist or not, u have to write similarly in your persist method.

          code:
          -----------

          UniqueConstraintChecker ucc = new UniqueConstraintChecker();

          try {
          if (!ucc.checkIfAlreadyExists("mobee_database", "db_monitor_name",
          doMobeeDatabase.getDb_monitor_name())) {

          Calendar cal = java.util.GregorianCalendar.getInstance();

          doMobeeDatabase.setLog_file_name("LogFileName_"
          + doMobeeDatabase.getDb_monitor_name());

          BeanUtils.copyProperties(getInstance(), doMobeeDatabase);

          getInstance()
          .setTime_up_from(
          (doMobeeDatabase.getTime_up_fromHours() + ":" + doMobeeDatabase
          .getTime_up_fromMinits()).trim());

          getInstance()
          .setTime_up_to(
          (doMobeeDatabase.getTime_up_toHours() + ":" + doMobeeDatabase
          .getTime_up_toMinits()).trim());

          getInstance().setCreated_on(DatabaseUtils.getCurrentDate());
          getInstance().setCreated_by(
          (String) Contexts.getSessionContext().get("userName"));
          log.info("before Persist");

          String s = super.persist();

          if (s.equals("persisted")) {
          updateServer();
          }

          } else {
          facesMessages.addToControl("dbMonitorName", "#{messages.dbmonitorname}: "
          + doMobeeDatabase.getDb_monitor_name()
          + " #{messages.alreadyexists}");
          return null;
          }
          } catch (Exception e) {
          e.printStackTrace();
          }



          -----------------
          UniqueConstraintChecker .java
          --------------------------------------------


          and this code write in your project in some where, after use this any class

          -------------------------------------------
          import java.util.ArrayList;

          import org.jboss.seam.framework.EntityController;
          import org.jboss.seam.log.LogProvider;
          import org.jboss.seam.log.Logging;

          public class UniqueConstraintChecker extends EntityController {

          private static LogProvider log = Logging.getLogProvider(UniqueConstraintChecker.class);

          public boolean checkIfAlreadyExists(String tableName, String columnName,
          String value) {
          boolean b = false;

          ArrayList al = new ArrayList();
          al = (ArrayList) createQuery(
          "from " + tableName +" where lower("+ columnName +")=:columnValue")
          .setParameter("columnValue", value.toLowerCase()).getResultList();

          if (al.size() > 0) {

          log.debug("size:::" + al.size());

          b = true;
          }

          log.debug("b:" + b);
          return b;
          }

          /*public boolean checkIfAlreadyExistsByChannelName(String tableName,
          String columnName, String value, String channelName,
          String channelValue) {
          boolean b = false;

          ArrayList al = new ArrayList();
          al = (ArrayList) createQuery(
          "from " + tableName + " where " + columnName
          + "=:columnValue and channelName=:channelValue")
          .setParameter("columnValue", value).setParameter(
          "channelValue", channelValue).getResultList();

          if (al.size() > 0) {

          log.debug("size:::" + al.size());

          b = true;
          }

          log.debug("b:" + b);
          return b;
          }*/

          public boolean checkIfAlreadyExistsLong(String tableName,
          String columnName, Long value) {
          boolean b = false;

          ArrayList al = new ArrayList();
          al = (ArrayList) createQuery(
          "from " + tableName + " where " + columnName + "=:columnValue")
          .setParameter("columnValue", value).getResultList();

          if (al.size() > 0) {
          b = true;
          }

          return b;
          }
          }

          • 2. Re: Server side validation
            gopi b Newbie

            Give me to reply and don't forget rating also.

            • 3. Re: Server side validation
              Shaiju CK Newbie

              Hi gopib,
              I am sorry, I guess you have not understood my issue.
              My concern was not the logic of checking uniquness of a user name.It got more to do with how the validation logic getting invoked using JSF(richfaces) and how the error message display back to the UI ?

              Thanks for putting effort
              Shaiju

              • 4. Re: Server side validation
                gopi b Newbie

                This is Xhtml page:
                ------------------------
                <h:panelGrid columns="1" cellspacing="0" width="100%"
                cellpadding="0" columnClasses="columnFull">
                <s:decorate for="dbMonitorName" template="/layout/edit.xhtml">
                <ui:define name="label">
                <h:outputText for="dbMonitorName" styleClass="formFont">#{messages.dbmonitorname}</h:outputText>
                </ui:define>

                <h:inputText id="dbMonitorName" required="true" maxlength="20"
                requiredMessage="#{messages.validatemonitorname}"
                rendered="#{!dbendPointHome.managed}" style=" width : 195px;"
                styleClass="formFont2"
                value="#{doMobeeDatabase.db_monitor_name}">
                </h:inputText>
                <h:inputText id="dbMonitorName1"
                value="#{doMobeeDatabase.db_monitor_name}"
                rendered="#{dbendPointHome.managed}" disabled="true"
                style=" width : 195px;" styleClass="formFont3">
                </h:inputText>
                </s:decorate>
                <s:decorate template="/layout/edit.xhtml">
                <h:outputText styleClass="formFont11"
                value="#{messages.exampleformat}"></h:outputText>
                </s:decorate>
                </h:panelGrid>

                ------------------------

                Entity:

                -----------------------------


                import org.jboss.seam.annotations.Name;

                @Entity(name = "mobee_database")
                public class MobeeDatabase implements Serializable {

                /**
                * @author Gopi Balagala
                */
                private static final long serialVersionUID = 271759555409098244L;
                private Long id;
                private String db_monitor_name;

                @Id@GeneratedValue(generator="database_Seq")
                @SequenceGenerator(name="database_Seq",sequenceName="MOBEE_DATABASE_SEQUENCE", allocationSize=1)
                public Long getId() {
                return id;
                }

                public void setId(Long id) {
                this.id = id;
                }

                @Column(name = "db_monitor_name", nullable = false, unique = true, length = 20)
                public String getDb_monitor_name() {
                return db_monitor_name;
                }

                public void setDb_monitor_name(String db_monitor_name) {
                this.db_monitor_name = db_monitor_name;
                }
                }


                -----------------------------------------

                Bean entity: for validation purpose:
                -----------------------------------------


                @Name("doMobeeDb")
                @Scope(ScopeType.CONVERSATION)
                public class DoMobeeDb implements Serializable {

                /**
                * @author janakiram
                */
                private static final long serialVersionUID = 254820506405596323L;

                @Length(min=4, max=255)
                @Pattern(regex="[a-zA-Z]+[a-zA-Z0-9\\_]+[a-zA-Z]+" , message="#{messages.validatestartswithlettersnumbers}")
                private String dbName;

                public String getDbName() {
                return dbName;
                }

                public void setDbName(String dbName) {
                this.dbName = dbName;
                }

                ---------------------------------










                • 5. Re: Server side validation
                  Shaiju CK Newbie

                  Hi Gopi,
                  I really could not understand what was the solution you have given.
                  It would be helpful, if you can provide a brief explanation.

                  Shaiju

                  • 6. Re: Server side validation
                    Nick Belaevski Master

                    Hi Shaiju,

                    You don't have to validate value inside some method called as ajaxListener. Take a look at documentation for rich:ajaxValidator.