简体   繁体   中英

How to use Jsf validator to see if a user name and/or email already exists

I have looked at a few threads on here like Check if username already exists in the register form

I am writing a login form and I am trying to figure out how to use the JSF validator to validate weither or not a userName or email address is already in the database. I already have the method in my POJO set up and it works fine through Java determaning weither or not they do exist. my problem lies on how to then get the message back to the screen and how to link it.

this is my XHTML:

  <!--UserName form group-->
                        <div class="form-group has-feedback" id="CAuserNameFormGroup">
                            <!-- -->
                            <div class="input-group" id="CAuserNameInputGroup">
                                <!-- -->
                                <span class="input-group-addon"><i class="glyphicon glyphicon-user" id="CAuserNameLeftGlyph"></i></span>
                                <!-- -->
                                <h:inputText class="form-control" id="CAuserName" value="#{accountCreationBean.userName}" pt:placeholder="#{accMsgs.userName}" required="true" requiredMessage="#{accMsgs.userNameRequired}" validator="#{accountCreationBean.validateEmail(context, component, value)}"></h:inputText>
                            </div>  
                             <span class="help-block"><h:message id="CAuserNameMessage" class="text text-danger" for="CAuserName" ></h:message></span>
                            <!-- -->
                            <span class="form-control-feedback" id="CAuserNameRightGlyph"></span>
                        </div>

My POJO (the database connection)

  public boolean addMember(String userName, String password, String title, String Phone, String email, String firstName, String lastName, String major)
    {
        String addQuery = "INSERT INTO member(First_Name, Last_Name, Phone, Email, Title, UserName, Password, Major) VALUES " + 
                "('" + firstName + "', '" + lastName + "', '" + Phone + "', '" + email + "', '" + title + "', '" + userName + "', '" + password + "', '" + major + "')";
        if(!doesExist(userName, email))
        {
            try
            {
                PreparedStatement addMember = connection.prepareStatement(addQuery);
                addMember.executeUpdate();
                return true;

            } 
            catch (SQLException ex)
            {
                Logger.getLogger(DBConnection.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        else
            System.out.println("Sorry, but the user " + userName + " and or the emial " + email + " is already regestered with us" );
        return false;

    }
    public boolean doesExist(String userName, String email)
    {
        userNameExsits = "";
        emailExists = "";
        try
        {
            String query = "SELECT UserName, Email FROM Member";
            statement = (Statement) connection.createStatement();
            resultSet = statement.executeQuery(query);

            while(resultSet.next())
            {
                if (resultSet.getString("UserName").equals(userName))
                {
                    userNameExsits = userName;
                }
                if(resultSet.getString("Email").equals(email))
                {
                    emailExists = email;
                }
                    return true;
            }
        } 
        catch (SQLException ex)
        {
            Logger.getLogger(DBConnection.class.getName()).log(Level.SEVERE, null, ex);
        }
        return false;
    }

and this is my Bean (Request Scoped)

  public void createAccount()
    {
        if(super.addMember(userName, password, title, phone, email, firstName, lastName, major))
              clear(); 
    }
    private void clear()
    {
        firstName = "";
        lastName = "";
        email = "";
        phone = "";
        title = "";
        userName = "";
        password = "";
        major = "";
    };

1.Validator: JSF supports data validation through the bean in the backend and the validation methods in the validator.

2.How to use: In the page(JSP), the input component can be associated with the method in the backend bean. For example,there is a bean named RegisterBean, and it has a method called validateEmail.So in the JSP page,you can write like this:

《h:inputText id= “emailInput” validator= “#{registrationBean.validateEmail}” value= “#{registrationBean.email}” /》

And then,once the user enters a email for this input component, the validateEmail() will check whether or not the email is already existed.IF not,the email in the bean will be updated.Otherwise, return error msg (MESSAGE).

HOPE THIS MAY HELP YOU.

you are using:

 validator="#{accountCreationBean.validateEmail(context, component, value)}"

but without showing the underlaying code.

Assuming, your validator is set up properly (which we can't know for reasons outlined), all you need to do is providing an Validation-error-message, which could be something like this:

<h:inputText 
   class="form-control" 
   id="CAuserName" 
   value="#{accountCreationBean.userName}" 
   pt:placeholder="#{accMsgs.userName}" 
   required="true" 
   requiredMessage="#{accMsgs.userNameRequired}" 
   validator="#{accountCreationBean.validateEmail(context, component, value)}"

   validatorMessage="An account with that email address already exists or the email address provided is malformated."
   >
</h:inputText>

Messages, generated like this are availble the same way you access the "requiredMessage" of the same field.

(Ie with a component framework used the right way, they are already "handled")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM