-2

I want to disable the submit button of my signup form if username exist in database i tried multiple solutions but no one works

my xhtml

<h:outputLabel for="#{userBean.username}" value="UserName" />
<p:inputText styleClass="input" id="username" value="#{userBean.username}" label="UserName " required="true"
    requiredMessage="Username is required"
    validatorMessage="UserName should be of maximum length 20 chars">
    <f:validateLength maximum="20"></f:validateLength>
    <p:ajax  event="keyup" listener="#{userBean.searchByUsername()}" update="usernamegrowl,signupButton" /> 
                </p:inputText>
<p:message  id="usernamemessage" for="username"></p:message>
<p:commandButton id="signupButton" value="SignUp"  action="#{userBean.persistUser()}"
                ajax="false" disabled="#{userBean.message}"></p:commandButton>

my method searchByUsername

public void searchByUsername() {
    User user = userService.findUserByUserName(getUsername());
    if (user != null) {
        FacesContext.getCurrentInstance().addMessage("usernamegrowl",
                new FacesMessage(FacesMessage.SEVERITY_ERROR, "username is alredy used", ""));
        RequestContext.getCurrentInstance().execute("document.getElementById('signupform:signupButton').setAttribute('disabled', 'true');");

        setMessage(false);
    }
    else
        setMessage(true);
    RequestContext.getCurrentInstance().execute("document.getElementById('signupform:signupButton').setAttribute('disabled', 'false');");

}
Cœur
  • 37,241
  • 25
  • 195
  • 267
khalil
  • 1

1 Answers1

0

update is space-separated.

For a detailed information about process/update and execute/render please read this answer.

Tonkichi
  • 251
  • 2
  • 7