4

验证器类:

@FacesValidator("br.gov.valec.sicpd.util.CpfValidator")
public class CpfValidator implements Validator {

    @Override
    public void validate(FacesContext context, UIComponent component, Object value)
        throws ValidatorException {
        if (validateCpf(value.toString())) {
            FacesMessage msg = new FacesMessage(FacesMessage.SEVERITY_ERROR,"Invalid Input","Invalid Input");
            ((UIInput) component).setValid(false); // this line doesnt work

        throw new ValidatorException(msg);
    }
}

JSF 片段:

<p:inputText label="CPF" id="inputCpf"
                    value="#{mainBean.owner.cpf}">
                    <f:validator validatorId="br.gov.valec.sicpd.util.CpfValidator" />
                    <p:ajax event="change" update="inputNameOwner"
                        listener="#{mainBean.searchOwner}"  />
</p:inputText>

当通过命令按钮 primefaces 提交表单时,它会自动突出显示。当 ajax 被触发并且验证失败时,我该如何实现呢?

4

1 回答 1

3

UIInput#setValid(false)工作正常。您只是忘记告诉 ajax 更新输入组件本身。添加inputCpf@this<p:ajax update>

<p:ajax ... update="@this inputNameOwner" />

顺便说一句,验证器中的显式UIInput#setValid(false)调用是不必要的。摆脱它。一旦 JSF 捕获了ValidatorException验证器抛出的问题,它就已经自己完成了所有工作。

于 2013-08-27T19:48:58.193 回答