Check if username already exists in the register form

可紊 提交于 2019-12-07 15:42:26

One way to do it is by submitting your input on blur and checking if it's duplicated. In case it's duplicated you will need to register a message specific for that component, in this case, I will bind the input to be able to access the component's id from my bean.

Here is a sample (and tested) code that contains the relevant elements you will need:

View

<h:form id="mainForm">
    <h:panelGrid id="grid" columns="3" cellpadding="4">  
        <p:outputLabel for="pseudo" value="Login" />
        <p:inputText id="pseudo" value="#{viewMBean.login}" binding="#{viewMBean.loginInput}" title="Pseudo" required="true" requiredMessage="The Pseudo field is required."
                     placeholder="Login">
            <p:ajax event="blur" listener="#{viewMBean.checkDuplicateLogin}" update="pseudoMsg" />
        </p:inputText>
        <p:message id="pseudoMsg" for="pseudo" />
    </h:panelGrid>
    <p:commandButton value="Register" />
</h:form>

Bean

import java.io.Serializable;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;

@ManagedBean
@ViewScoped
public class ViewMBean implements Serializable {

    private UIComponent loginInput;

    private String login;

    public void checkDuplicateLogin() {
        //replace with your own fancy code
        if (login.equals("admin")) {
            FacesContext context = FacesContext.getCurrentInstance();
            context.addMessage(loginInput.getClientId(), new FacesMessage(FacesMessage.SEVERITY_ERROR, "Duplicated Login", null));
        }
    }

    public String getLogin() {
        return login;
    }

    public void setLogin(String login) {
        this.login = login;
    }

    public UIComponent getLoginInput() {
        return loginInput;
    }

    public void setLoginInput(UIComponent loginInput) {
        this.loginInput = loginInput;
    }

}

Make changes to addCustomer() method as below . I've added comments for you to get an idea.

public String addCustomer() throws IOException {
        //you don't need password to check though
        boolean isExist = accountBusinessLocal.authentificateUser(login,password);
        if(isExist){
        //return empty
        FacesContext context = FacesContext.getCurrentInstance(
        context.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "Duplicated Login", null));
        return "";          
        }

        logger.log(Level.SEVERE, "*****add customer***** ");
        creation = new Date();
        right = Right.SimpleUser;
        Customer customerRegister = customerLocal.addUser(name, familyName, address, email, right, creation, organisation);
        accountBusinessLocal.addAccount(login, password, customerRegister);
        return "login.jsp";
    }

Update: In order to show message ,add p:message tag inside the form.

<p:messages id="messages" showDetail="true" autoUpdate="true" closable="true" />  
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!