首页 > 解决方案 > 如何使用 ajax 事件和渲染消息通过 @FacesValidator 执行单个输入验证?

问题描述

用法:JSF 2.3.2 Mojarra

在将 ajax 添加到我的 jsf 时,我遇到了这样的情况:

无法刷新:h:message id="phoneNumber-msg"在填写表单期间

<h:messages id="validation-messages" styleClass="validation-messages"/>
<h:form>
    <h:outputLabel for="phoneNumber">Phone number</h:outputLabel>
    <h:inputText id="phoneNumber" value="#{bean.phoneNumber}">
        <f:validator validatorId="validators.PhoneNumber"/>
        <f:ajax event="blur" execute="@this" render="phoneNumber-msg"/>
    </h:inputText>
    <h:message id="phoneNumber-msg" for="phoneNumber"/>
    [...]
    <h:commandButton value="Submit" action="#{userDetails.submit}"/>
</h:form>

validators.PhoneNumber是一个@FacesValidator:

@FacesValidator("validators.PhoneNumber")
public class PhoneNumberValidator implements javax.faces.validator.Validator {
    @Override
    public void validate(FacesContext context, UIComponent component, Object value) throws ValidatorException { ... }
}

此外

当通过结束表单的“提交”按钮刷新时,验证工作正常(来自validators.PhoneNumber的消息同时出现在:id="validation-messages"id="phoneNumber-msg" ):

   <h:commandButton value="Submit" action="#{userDetails.submit}"/>
</h:form>

如果您考虑不同的验证方式 - > 在 EmailAddress 输入中,我使用了我自己的类注释(EmailAddressValidator 实现 ConstraintValidator),它通过表单中使用的 bean 上的 @ValidEmailAddress 进行验证 - 它正确呈现电子邮件验证消息,但在这里我想以不同的方式验证电话号码。


是否可以呈现id="phoneNumber-msg"(这意味着在填写表单期间进行验证器。电话号码?

标签: ajaxjsf

解决方案


推荐阅读