12 Replies Latest reply: Oct 15, 2012 5:00 AM by RMishra RSS

    How to write custom Error messges in profileFormHandler

    967509
      Hi,
      i am using profileFormHandler to register user and i need to validate the password field in it. whether it has the 10 chars or not. here i am calling one JAVA class named REG.java which is extending ProfileFormHandler and in that i want to validate that password field and return the user-defiened error message again to that JSP page .
        • 1. Re: How to write custom Error messges in profileFormHandler
          RMishra
          Suppose you are using condition like:
          if(conditaion fail){
          addFormException(new DropletException("Your message");
          }

          You can use above line, where you are validating name in your formhandler. addFormException(new DropletException("........")) method available in GenericService and ProfileFormHandler's super hierarchy This class is available so we can use it in your formhandler.

          registration page use like:
          <dsp:page>
          ...................
          <dsp:form............>
          <dsp:droplet name="ProfileErrorMessageForEach">
          <dsp:param bean="yourformhandler.formExceptions" name="exceptions"/>
          <dsp:oparam name="output">
          <dsp:valueof param="message"/>
          </dsp:oparam>                                   
          </dsp:droplet>                                   
          ............................
          your registration entries
          ...........
          </dsp:form>
          <dsp:page>

          -RMishra

          Edited by: RMishra on Oct 12, 2012 5:21 PM
          • 2. Re: How to write custom Error messges in profileFormHandler
            967509
            thanks for the reply,
            ya i too written like the way you told ! can we able to get that error message which is written in addFormException(new DropletException("password exception")) to the jsp page? because i cant able to get those.
            and also in my custom formhandler which extends ProfileFormHandler do i have to include PreCreateUser() method for validating password or can i write it in handleCreate(...) method?
            pls answer.

            Edited by: 964506 on Oct 12, 2012 5:07 AM

            Edited by: 964506 on Oct 12, 2012 5:13 AM
            • 3. Re: How to write custom Error messges in profileFormHandler
              RMishra
              yes, In my recent project also I have done like this.
              -RMishra
              • 4. Re: How to write custom Error messges in profileFormHandler
                RMishra
                you should write your validation logic in preCreateUser and add your error messege using above method in right place in preCreateUser(). we should not have any custom logic in handlemethod but such validation logic should go to pre.... methods.

                -RMishra

                Edited by: RMishra on Oct 12, 2012 5:49 PM
                • 5. Re: How to write custom Error messges in profileFormHandler
                  967509
                  i am pasting my code could you please verify it!

                  Registration.jsp

                  <%@ taglib uri="/dspTaglib" prefix="dsp" %>
                  <dsp:importbean bean="/atg/userprofiling/ProfileFormHandler"/>
                  <dsp:importbean bean="/atg/userprofiling/ProfileErrorMessageForEach"/>
                  <dsp:page>

                  <dsp:droplet name="/atg/dynamo/droplet/Switch">
                  <dsp:param bean="ProfileFormHandler.formError" name="value"/>
                  <dsp:oparam name="true">
                  <font color=cc0000><STRONG><UL>
                  <dsp:droplet name="ProfileErrorMessageForEach">
                  <dsp:param bean="ProfileFormHandler.formExceptions" name="exceptions"/>
                  <dsp:oparam name="output">
                       <LI> <dsp:valueof param="message"/>
                  </dsp:oparam>
                  </dsp:droplet>
                  </UL></STRONG></font>
                  </dsp:oparam>
                  </dsp:droplet>

                          
                  <dsp:a href="../index.jsp"> Cancel
                  </dsp:a>


                  <dsp:form action="Registration.jsp" method="POST" >
                  Please Fill the information :<br>


                       <p>First Name :<dsp:input type="text" bean="ProfileFormHandler.value.firstName" required="true" />
                       <p>Middle Name:<dsp:input type="text" bean="ProfileFormHandler.value.middleName" required="true" />
                       <p>Last Name :<dsp:input type="text" bean="ProfileFormHandler.value.lastName" required="true" />
                       <p>Login Name :<dsp:input type="text" bean="ProfileFormHandler.value.login" required="true" />
                       <p>Email ID :<dsp:input type="text" bean="ProfileFormHandler.value.email" required="true" />
                       
                       <p>Password :<dsp:input type="text" bean="ProfileFormHandler.value.password" required="true" />
                       <p>Gender Male :<dsp:input type="radio" bean="ProfileFormHandler.value.gender" value="male" checked="true"/>
                       Female:<dsp:input type="radio" bean="ProfileFormHandler.value.gender" value="female" />
                       
                       <p>Date of Birth: <dsp:input type="text" bean="ProfileFormHandler.value.dateOfBirth" date="MM/dd/yyyy" required="true" />
                       <dsp:input type="hidden" bean="ProfileFormHandler.createSuccessURL"     value="./Home.jsp"/>
                  <dsp:input type="submit" bean="ProfileFormHandler.create"      value="Register"/>     
                  </dsp:form>
                  </dsp:page>


                  and corresponding class which is extending ProfileFormHandler is
                  RegisterFormHandler.java

                  package syntel;
                  import java.io.IOException;

                  import javax.servlet.ServletException;

                  import atg.droplet.DropletException;
                  import atg.servlet.DynamoHttpServletRequest;
                  import atg.servlet.DynamoHttpServletResponse;
                  import atg.userprofiling.ProfileFormHandler;

                  public class RegisterFormHandler extends ProfileFormHandler {
                       
                  @Override
                  protected void preCreateUser(DynamoHttpServletRequest pRequest,
                            DynamoHttpServletResponse pResponse) throws ServletException,
                            IOException {
                       // TODO Auto-generated method stub
                  int f=Integer.parseInt((String) getValue().get("password"));
                       if(f!=10)
                       {
                            addFormException(new DropletException("password length should be 10"));
                       }
                            
                       super.preCreateUser(pRequest, pResponse);
                  }
                       @Override
                       public boolean handleCreate(DynamoHttpServletRequest pRequest,
                                 DynamoHttpServletResponse pResponse) throws ServletException,
                                 IOException {
                            // TODO Auto-generated method stub
                            return true;
                       }
                  }

                  could you please check where i am i doing wrong?
                  • 6. Re: How to write custom Error messges in profileFormHandler
                    967509
                    i am pasting my code could you please verify it!

                    Registration.jsp

                    <%@ taglib uri="/dspTaglib" prefix="dsp" %>
                    <dsp:importbean bean="/atg/userprofiling/ProfileFormHandler"/>
                    <dsp:importbean bean="/atg/userprofiling/ProfileErrorMessageForEach"/>
                    <dsp:page>

                    <dsp:droplet name="/atg/dynamo/droplet/Switch">
                    <dsp:param bean="ProfileFormHandler.formError" name="value"/>
                    <dsp:oparam name="true">
                    <font color=cc0000><STRONG><UL>
                    <dsp:droplet name="ProfileErrorMessageForEach">
                    <dsp:param bean="ProfileFormHandler.formExceptions" name="exceptions"/>
                    <dsp:oparam name="output">
                         <LI> <dsp:valueof param="message"/>
                    </dsp:oparam>
                    </dsp:droplet>
                    </UL></STRONG></font>
                    </dsp:oparam>
                    </dsp:droplet>

                            
                    <dsp:a href="../index.jsp"> Cancel
                    </dsp:a>


                    <dsp:form action="Registration.jsp" method="POST" >
                    Please Fill the information :<br>


                         <p>First Name :<dsp:input type="text" bean="ProfileFormHandler.value.firstName" required="true" />
                         <p>Middle Name:<dsp:input type="text" bean="ProfileFormHandler.value.middleName" required="true" />
                         <p>Last Name :<dsp:input type="text" bean="ProfileFormHandler.value.lastName" required="true" />
                         <p>Login Name :<dsp:input type="text" bean="ProfileFormHandler.value.login" required="true" />
                         <p>Email ID :<dsp:input type="text" bean="ProfileFormHandler.value.email" required="true" />
                         
                         <p>Password :<dsp:input type="text" bean="ProfileFormHandler.value.password" required="true" />
                         <p>Gender Male :<dsp:input type="radio" bean="ProfileFormHandler.value.gender" value="male" checked="true"/>
                         Female:<dsp:input type="radio" bean="ProfileFormHandler.value.gender" value="female" />
                         
                         <p>Date of Birth: <dsp:input type="text" bean="ProfileFormHandler.value.dateOfBirth" date="MM/dd/yyyy" required="true" />
                         <dsp:input type="hidden" bean="ProfileFormHandler.createSuccessURL"     value="./Home.jsp"/>
                    <dsp:input type="submit" bean="ProfileFormHandler.create"      value="Register"/>     
                    </dsp:form>
                    </dsp:page>


                    and corresponding class which is extending ProfileFormHandler is
                    RegisterFormHandler.java

                    package syntel;
                    import java.io.IOException;

                    import javax.servlet.ServletException;

                    import atg.droplet.DropletException;
                    import atg.servlet.DynamoHttpServletRequest;
                    import atg.servlet.DynamoHttpServletResponse;
                    import atg.userprofiling.ProfileFormHandler;

                    public class RegisterFormHandler extends ProfileFormHandler {
                         
                    @Override
                    protected void preCreateUser(DynamoHttpServletRequest pRequest,
                              DynamoHttpServletResponse pResponse) throws ServletException,
                              IOException {
                         // TODO Auto-generated method stub
                    int f=Integer.parseInt((String) getValue().get("password"));
                         if(f!=10)
                         {
                              addFormException(new DropletException("password length should be 10"));
                         }
                              
                         super.preCreateUser(pRequest, pResponse);
                    }
                         @Override
                         public boolean handleCreate(DynamoHttpServletRequest pRequest,
                                   DynamoHttpServletResponse pResponse) throws ServletException,
                                   IOException {
                              // TODO Auto-generated method stub
                              return true;
                         }
                    }

                    could you please check where i am i doing wrong?
                    • 7. Re: How to write custom Error messges in profileFormHandler
                      RMishra
                      you can create like :

                      package atg.userprofiling;

                      public class ProfileFormHandler extends ProfileFormHandler
                      {
                      protected void preCreateUser(........................)throws ServletException, IOException
                      {
                      String p=(String) getValue().get("password")
                      if(p.length()!=10){
                      addFormException(new DropletException("password length should be 10"));
                      }
                      }

                      then create /atg/userprofiling/ProfileFormHandler.properties set
                      $class=atg.userprofiling.ProfileFormHandler
                      ..........


                      if you will create RegisterFormHandler then you have to create RegisterFormHandler.properties and you have to use RegisterFormHandler.value.firstName like to all so above is good way.
                      We can overridge any OOTB component with same name by extending same class as above so if you want to create RegisterFormHandler extends ProfileFormHadler, then you should do like
                      public class RegisterFormHandler extends atg.userprofiling.ProfileFormHandler{
                           protected void preCreateUser(DynamoHttpServletRequest pRequest, DynamoHttpServletResponse pResponse)throws ServletException, IOException
                           {
                           String p=(String) getValue().get("password")
                           if(p.length()!=10){
                           addFormException(new DropletException("password length should be 10"));
                           }     
                      }


                      Note: in both cases when we will call handleCreate it will automatically call your preCreateUser method.

                      -RMishra
                      • 8. Re: How to write custom Error messges in profileFormHandler
                        967509
                        thank you very much i got it
                        Mishra
                        • 9. Re: How to write custom Error messges in profileFormHandler
                          967509
                          Hello,
                          could you please explain what exactly these lines will do i really cant able to understand !
                          Synchronized (getProfile()) {
                          preCreateUser(pRequest,pResponse);
                          return super.handleCreate(pRequest, pResponse);
                          }

                          why we are using synchronized here?
                          what getProfile() means?
                          why we are calling preCreateUser here? what will happen if we didnt call that?
                          what return super.handlerCreate(....) will do?
                          could you please answer me with patience!!!

                          Thanks in advance!
                          • 10. Re: How to write custom Error messges in profileFormHandler
                            RMishra
                            these lines are part of profileformhandler that i have written by mistake. :( . I think no need to override handlecreate method only overriding precreateuser in enough for your requirement.
                            In profileformhandler's handleCreate method:
                            using synchronized block will allow only one thread to use this block at a time.
                            getProfile() will return instance of type Profile.
                            preCreateUser available in ProfileForm's handlecreate method which will be called before any db logic will execute. profileformhandler extends profileform so calling super.handlerCreate from handlecreate of profileformhandler will call handlecreate method of profileform.
                            Hope it helps.
                            -RMishra
                            • 11. Re: How to write custom Error messges in profileFormHandler
                              967509
                              ya as you told you've written those line by mistake!

                              but when it comes to redirecting to successurl which we'll specify in jsp page after all validations done , after including that codes only its redirecting ! otherwise it's not!
                              if i really wants to skip those codes ! what i've to write in handlerCreate(...) method to redirect?

                              Edited by: 964506 on Oct 15, 2012 2:35 AM
                              • 12. Re: How to write custom Error messges in profileFormHandler
                                RMishra
                                I think no need to override handleCreate method at all. Because if you will extends profileformhandler, then handlecreate method will automatically be available in our custom formhandler and when we will call ourXXXFormHandler.create, then it should automatically call to super class handle method.
                                If we need to chand anything in handle method except pre... and post.... then only we should override handleXXX method.
                                -RMishra