2012-09-28 169 views
1

我试图只显示一个两个所需的字段。 目前有两个错误消息,如果两个字段都是空的。我想实现只有一个字段为空的情况下只有一条消息。JSF两个所需输入字段的一个错误消息

的代码看起来是这样的:

<x:inputText 
    value="#{bean.proxyUrl}" 
    id="idProxyUrl" 
    required="true" 
    /> 
<x:outputText value=":" /> 
<x:inputText 
    value="#{bean.proxyPort}" 
    id="idProxyPort" 
    required="true" 
    /> 
<x:message for="idProxyUrl" errorClass="errorMessage" style="margin-left: 10px;" /> 
<x:message for="idProxyPort" errorClass="errorMessage" style="margin-left: 10px;" /> 

我能做些什么,我只得到一个消息,无论该领域的一个或两个是空的。

+0

'x:'前缀不可识别为任何已知的JSF组件库。我是否可以假定它是使用URI“http:// java.sun.com/jsf/html”设置的标准JSF HTML组件? (如果是这样,你为什么要改变世界上每个人都使用的标准'h:'前缀?) – BalusC

+1

这可能会帮助你一点... http://stackoverflow.com/q/10007438/617373 – Daniel

+0

'x :'指向'http:// myfaces.apache.org/tomahawk' – Przemek

回答

2

您可以为检查第一个组件的SubmittedValue的第二个组件指定一个特殊的验证程序。我为PasswordValidator做了类似的检查相应的确认密码字段。

@FacesValidator("passwordValidator") 
public class PasswordValidator implements Validator {  

    @Override 
    public void validate(FacesContext context, UIComponent component, 
      Object value) throws ValidatorException { 


     String password = (String) value; 


     UIInput confirmComponent = (UIInput) component.getAttributes().get("confirm"); 
     String confirm = (String) confirmComponent.getSubmittedValue(); 

     if (password == null || password.isEmpty() || confirm == null || confirm.isEmpty()) { 
      FacesMessage msg = new FacesMessage(FacesMessage.SEVERITY_ERROR, "Please confirm password", null); 
      throw new ValidatorException(msg); 
     } 


     if (!password.equals(confirm)) { 
      confirmComponent.setValid(false); 
      FacesMessage msg = new FacesMessage(FacesMessage.SEVERITY_ERROR, "The entered passwords do not match", null); 
      throw new ValidatorException(msg); 
     } 


    } 

您必须检查其他组件的提交值的原因是验证程序在生命周期的过程验证阶段被调用。直到此阶段完成并且每个提交的值已通过验证后,才会应用所提交的值。

+0

谢谢。经过一些小小的改变,因为这些领域不一定是平等的,这就是帮助。我对JSF相当陌生,需要非常学习! – Przemek