2 Replies Latest reply on Nov 11, 2010 5:17 AM by Nikos Paraskevopoulos

    How to control objects with a validator?

    Benjamin Mark Newbie

      Hello,


      I'm a little bit confused how to solve the following problem.


      I have a form with an email field. With the email field a custom validator is attached which is checking the format and if the email is already used in the database. This is working quite perfekt. Now I wan't to implement a help for the user: A link should appear which is guiding the user to the password forgotten page. Currently only a message appears that the email is already used.


      How can I enable that link with the validator?


      Thx for your help


      Best regards


      Ben


      <s:decorate id="emailField" template="layout/edit.xhtml">
         <ui:define name="label">#{messages.text_email}</ui:define>
         <h:inputText id="email" required="true" size="45" maxlength="45" label="#{messages.text_email}" value="#{personHome.instance.email}">
         <f:validator validatorId="emailValidator" />
         <!-- <t:validateEmail /> -->     
         </h:inputText>
      </s:decorate>
      


        • 1. Re: How to control objects with a validator?
          Todd Morrison Newbie

          Seems like you have a validator that is custom.


          Hibernate Validation will fire off a default message during that stage of the seam request, but the check for existing email addresses sounds like code that you are responsible for (or someone on your team).


          I would hunt down the code that does this and modify the message. It is common to create a validator annotation to decorate properties on the component; often developers will allow the validation message to be added via constructor on this annotation. This type of validation could also be done on your Domain Entity, so check there too.



          If you can give more information about where the email-existence check is being done, perhaps I could help more.



          Todd


          • 2. Re: How to control objects with a validator?
            Nikos Paraskevopoulos Novice

            Hi,


            I believe that what you ask should not be part of a JSF validator. JSF validators implement simple, single field validatation. E.g. has this string a valid email format? (contains @ sign, account and domain etc). When a JSF validator fails, the model is not updated. So if you enter '123' at the email field, the #{personHome.instance.email} will retain its old value at the server and an error message will appear at the client.


            I would suggest something like:


            <s:decorate FROM ABOVE, ONLY DIFFERENCES SHOWN>
              <h:inputText ...
                valueChangeListener="#{someBean.emailChanged}"
              />
            </s:decorate>
            <h:commandLink
              rendered="#{someBean.emailExistsInDbFlag}"
              action="#{some.action}"
              value="Forgot password?"
            />
            



            How it works: Initially someBean.emailExistsInDbFlag is false, so the Forgot password? link is hidden. If the user enters an invalid email (e.g. '123') the someBean.emailExistsInDbFlag field stays false and the link remains hidden, but an error message appears.


            If the email is correctly formatted, then the valueChangeListener fires. It searches the DB for the email and sets someBean.emailExistsInDbFlag accordingly. If the email exists, then the link is dhown.


            A corner case this code does not handle is when the user enters a valid, existing email first, then an invalid ('123') email. The change listener will not fire, leaving the Forgot password? link displayed together with the error message.


            It is easy to add AJAX behaviour to the above.


            Hope it helps.