我想禁用我的注册表单的提交按钮如果数据库中存在用户名我试过多个解决方案但是没有人工作
我的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>
我的方法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');");
}