验证器类:
@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片段:
JSF snippet:
<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提交自动突出显示它。我怎样才能做到这一点,当阿贾克斯被触发并且验证失败?
When the form is submitted via command button primefaces highlights it automatically. How can I achieve that when ajax is fired and validation fails?
在 UIInput#setValid(假)
工作正常。你刚才忘记告诉Ajax来更新输入组件本身。添加 inputCpf
或 @this
到&LT;电话号码:AJAX更新&GT;
。
The UIInput#setValid(false)
is working fine. You just forgot to tell ajax to update the input component itself. Add inputCpf
or @this
to <p:ajax update>
.
<p:ajax ... update="@this inputNameOwner" />
在校验明确 UIInput#setValid(假)
呼叫的方式是不必要的。摆脱它。一旦抓到的JSF已经做了一切本身的 ValidatorException
抛出您的验证。
That explicit UIInput#setValid(false)
call in validator is by the way unnecessary. Get rid of it. JSF already does it all by itself once it catches the ValidatorException
thrown by your validator.