PrimeFaces输入组件未在验证错误时突出显示

时间:2016-02-08 14:29:55

标签: validation jsf primefaces highlight

我正在使用Seam 2.3.1 Final。我在表单上添加了Custom EmailValidation。

@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 个答案:

答案 0 :(得分:3)

您还需要明确覆盖ajax-update中的输入。一种方法是添加代表当前表单的@form

<p:commandButton ... update=":tableForm @form" />

或通过其明确的ID。

<p:commandButton ... update=":tableForm :editPersonelForm" />

另一种方法是使用PFS/jQuery selector仅引用输入。

<p:commandButton ... update=":tableForm @(#editPersonelForm :input)" />

完全不同的方法是使用OmniFaces <o:highlight>将PrimeFaces自己的样式表放在关联的输入(和标签)上,这样您就不必担心明确地更新它们了。

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