Validation 如何在自定义验证器中获取另一个组件的值?

Validation 如何在自定义验证器中获取另一个组件的值?,validation,jsf,components,Validation,Jsf,Components,我使用自定义验证器。困难在于我只需要检查两个字段inputText并比较它们。第一个字段必须大于第二个字段。如果不是,那么我必须显示一条包含错误信息的消息。因此,我需要在我的自定义验证器中传递第一个inputText字段的值。为此,我需要读取验证器类中第一个InputText字段的值。如何在验证器类中获取必要组件的id?使用标签的解决方案不适合我。我需要直接转到所需的组件,也许这可以通过FacesContext的任何方法完成?只需通过传递整个组件即可 <h:form id="formId"

我使用自定义验证器。困难在于我只需要检查两个字段inputText并比较它们。第一个字段必须大于第二个字段。如果不是,那么我必须显示一条包含错误信息的消息。因此,我需要在我的自定义验证器中传递第一个inputText字段的值。为此,我需要读取验证器类中第一个InputText字段的值。如何在验证器类中获取必要组件的id?使用标签的解决方案不适合我。我需要直接转到所需的组件,也许这可以通过FacesContext的任何方法完成?

只需通过
传递整个组件即可

<h:form id="formId">
    <h:inputText value="#{bean.start}">
        <f:validator validatorId="rangeValidator" />
        <f:attribute name="endComponent" value="#{endComponent}" />
    </h:inputText>
    ...
    <h:inputText binding="#{endComponent}" value="#{bean.end}" />
    ...
</h:form>
public void validate(FacesContext fc, UIComponent uic, Object o) throws ValidatorException {
    String newPassword = fc.getExternalContext().getRequestParameterMap().get("centerForm:newPassword");
    String newPassword2 = (String) o;
    if(!newPassword.equals(newPassword2)){
        FacesMessage msg = new FacesMessage(FacesMessage.SEVERITY_ERROR,"New Passwords do not match", null);            
        throw new ValidatorException(msg);            
    }
}
重要注意事项是,组件将按照它们在树中出现的顺序进行处理、转换和验证。未转换/验证的组件的任何提交值可通过
UIInput#getSubmittedValue()
获得,而已转换/验证的组件的任何提交值可通过
UIInput#getValue()
获得。因此,在您的特定示例中,应该通过
UIInput#getSubmittedValue()
而不是
UIInput#getValue()
获取值

public void validate(FacesContext fc, UIComponent uic, Object o) throws ValidatorException {
    String newPassword = fc.getExternalContext().getRequestParameterMap().get("centerForm:newPassword");
    String newPassword2 = (String) o;
    if(!newPassword.equals(newPassword2)){
        FacesMessage msg = new FacesMessage(FacesMessage.SEVERITY_ERROR,"New Passwords do not match", null);            
        throw new ValidatorException(msg);            
    }
}
如果要使用
UIInput#getValue()
提供的已转换和验证的值,则需要将验证器移动到第二个组件,然后将第一个组件传递

<h:form id="formId">
    <h:inputText binding="#{startComponent}" value="#{bean.start}" />
    ...
    <h:inputText value="#{bean.end}" />
        <f:validator validatorId="rangeValidator" />
        <f:attribute name="startComponent" value="#{startComponent}" />
    </h:inputText>
    ...
</h:form>
public void validate(FacesContext fc, UIComponent uic, Object o) throws ValidatorException {
    String newPassword = fc.getExternalContext().getRequestParameterMap().get("centerForm:newPassword");
    String newPassword2 = (String) o;
    if(!newPassword.equals(newPassword2)){
        FacesMessage msg = new FacesMessage(FacesMessage.SEVERITY_ERROR,"New Passwords do not match", null);            
        throw new ValidatorException(msg);            
    }
}
另见:

您可以使用输入字段的name属性从请求参数映射中获取另一个字段的值。要获取输入字段的name属性,请查看源代码以查看生成的内容。见下面的例子

public void validate(FacesContext fc, UIComponent uic, Object o) throws ValidatorException {
    String newPassword = fc.getExternalContext().getRequestParameterMap().get("centerForm:newPassword");
    String newPassword2 = (String) o;
    if(!newPassword.equals(newPassword2)){
        FacesMessage msg = new FacesMessage(FacesMessage.SEVERITY_ERROR,"New Passwords do not match", null);            
        throw new ValidatorException(msg);            
    }
}

不幸的是,我建议阅读此解决方案不适合我,因为使用并不能解决我的问题。我需要使用组件id直接在Validator类中获取组件的值