【问题标题】:PrimeFaces input components are not highlighted on validation errorPrimeFaces 输入组件在验证错误时未突出显示
【发布时间】:2016-05-18 06:11:18
【问题描述】:

我正在使用 Seam 2.3.1 Final。我已经在我的表单上添加了自定义 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>

它有效,当我输入无效的电子邮件时,它会给出错误消息文本。但是,输入字段不会切换错误状态模式。输入不再有红线边框。

【问题讨论】:

  • 当您在update 中明确包含输入时也不行吗?哪个 PrimeFaces 版本?
  • 谢谢! update=":tableForm :editPersonelForm"解决我的问题!
  • 我想知道,editPersonelForm 在对话框中会导致重新渲染问题吗?或者自定义验证给重新渲染带来麻烦。但是,您的建议有效...如果您想写为答案,我会检查,或者我会写答案..
  • 我已经在半小时前发布了。也许你忘了 F5?
  • 你是对的......顺便说一句,这是Seam 2.3和PrimeFaces 5.3中电子邮件验证的一个例子......我之前没有找到一个完整的例子。它可以帮助其他人寻找。谢谢。

标签: validation jsf primefaces highlight


【解决方案1】:

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

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

或通过其显式 ID。

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

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

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

一种完全不同的方法是使用 OmniFaces &lt;o:highlight&gt; 将 PrimeFaces 自己的样式表放在相关的输入(和标签)上,这样您就不必担心显式地对它们进行 ajax 更新。

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-12-03
    • 2011-08-24
    • 1970-01-01
    • 1970-01-01
    • 2023-03-27
    • 1970-01-01
    • 2012-06-26
    • 1970-01-01
    相关资源
    最近更新 更多