`

Jsf自定义validator

    博客分类:
  • Jsf
阅读更多

 

页面:val为faces-config中配置的backbean

<h:form>
    <h:inputText id="email" required="true"  value="#{val.email}"
     validator="#{val.validateEmail}">
               <!-- <f:validateDoubleRange minimum="0" maximum="24" /> --> 
     </h:inputText>
            <h:message for="email" errorStyle="color:red"/>
    <h:commandButton value="summit"></h:commandButton>
   </h:form>

 

 

backbean中的验证方法, 方法必须有三个参数,如下

 

 

public void validateEmail(FacesContext facesContext,UIComponent component, Object newValue) throws ValidatorException {  
  String email = newValue.toString();  
  if (email.equals("4")) { // "".equals(email)  
  ((UIInput)component).setValid(false);  
  FacesMessage message = new FacesMessage("Email address is required.");  
  facesContext.addMessage(component.getClientId(facesContext),message);  
  
  }  
  } 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics