验证器类:
@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 被触发并且验证失败时,我该如何实现?
最佳答案
UIInput#setValid(false)
工作正常。您只是忘记告诉 ajax 更新输入组件本身。添加 inputCpf
或 @this
至 <p:ajax update>
.
<p:ajax ... update="@this inputNameOwner" />
那个露骨的
UIInput#setValid(false)
顺便说一下,调用验证器是不必要的。摆脱它。一旦 JSF 捕获到 ValidatorException
,它就已经自己完成了所有工作。由您的验证器抛出。
关于ajax - 当 ajax 请求验证失败时,如何使用 Primefaces 突出显示 UIInput?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18474140/