PrimeFaces输入组件在验证错误时没有高亮显示。

3

我正在使用Seam 2.3.1最终版。我已经在我的表单上添加了自定义电子邮件验证。

@Name("emailValidator")
@BypassInterceptors
@org.jboss.seam.annotations.faces.Validator
public class EmailValidator implements Validator {
    private static final String EMAIL_REGEX = "^[_A-Za-z0-9-]+(\\.[_A-Za-z0-9-]+)*@[A-Za-z0-9-]+(\\.[A-Za-z0-9-]+)*(\\.[A-Za-z]{2,})$";

    /**
     * <a href="http://www.mkyong.com/regular-expressions/how-to-validate-email
     * -address-with-regular-expression/">Source</a> <br/>
     * Modification : autorisation des "-" dans le nom de domaine <br/>
     * Exemple valide : jean-michel-75440.exemple42@email-pro.mon-entreprise.com
     */
    public void validate(FacesContext context, UIComponent component,
                         Object value) throws ValidatorException {

        /* Create the correct mask */
        Pattern mask = Pattern.compile(EMAIL_REGEX);

        /* Get the string value of the current field */
        String emailField = (String) value;

        /* Check to see if the value is a valid email */
        Matcher matcher = mask.matcher(emailField);

        if (!matcher.matches()) {
            FacesMessage message = new FacesMessage();
            message.setDetail("E-posta adresi geçerli değil!");
            message.setSummary("E-posta Hatasi");
            message.setSeverity(FacesMessage.SEVERITY_ERROR);

            throw new ValidatorException(message);
        }
    }

    public String getValidatorId() {
        return "emailValidator";
    }
}

同时,JSF

 <h:form id="editPersonelForm">

    <p:messages showDetail="true" autoUpdate="true" closable="true"/>

        <p:outputLabel for="personName" value="Ad"/>
        <p:inputText id="personName" placeholder="Ad" value="#{personelBean.personel.name}" required="true"
                     requiredMessage="Ad alanını doldurmak zorunludur." validatorMessage="Ad alanı zorunludur.">


        <p:outputLabel for="personEmail" value="E-Posta"/>
        <p:inputText id="personEmail" value="#{personelBean.personel.email}" placeholder="E-Posta" >
            <f:validator validatorId="emailValidator" />
        </p:inputText>

        <p:outputLabel for="personelSaveBtn" value=""/>
        <p:commandButton id="personelSaveBtn" value="Kaydet"
                         action="#{personelBean.saveOrPersist}"
                         oncomplete="if (args &amp;&amp; !args.validationFailed) PF('personEdit').hide();"
                         update=":tableForm" ajax="true">

        </p:commandButton>
    </p:panelGrid>
</h:form>

当我输入无效的电子邮件时,它会显示错误信息。然而,输入字段没有切换到错误状态模式。输入框中不再有红线边框。


1
还是说当您在update中明确包含输入时也会出现此问题?PrimeFaces版本是哪个? - BalusC
谢谢!update=":tableForm :editPersonelForm"解决了我的问题! - efirat
我在想,editPersonelForm是否在对话框内会导致重新渲染的问题?或者自定义验证会引起重新渲染的麻烦。不过,你的建议很有效...如果你想写成答案,我会检查一下,或者我会写答案。 - efirat
我已经半个小时前发布了它。也许你忘记按F5刷新了? - BalusC
你是正确的...顺便说一下,这是Seam 2.3和PrimeFaces 5.3中电子邮件验证的一个示例...我以前没有找到完整的示例。它对其他寻找帮助的人可能有所帮助。谢谢。 - efirat
1个回答

4
你需要明确地覆盖ajax-update中的输入内容。一种方法是添加@form,它代表当前表单。
<p:commandButton ... update=":tableForm @form" />

或者通过它的明确ID。
<p:commandButton ... update=":tableForm :editPersonelForm" />

另一种方法是使用 PFS/jQuery选择器 来仅引用输入内容。
<p:commandButton ... update=":tableForm @(#editPersonelForm :input)" />

完全不同的方法是使用OmniFaces <o:highlight> 将PrimeFaces自己的样式表放在相关输入框(和标签)上,这样您就永远不需要担心明确地进行ajax更新。

<o:highlight styleClass="ui-state-error" />

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接