PrimeFaces输入组件在验证错误时未突出显示 [英] PrimeFaces input components are not highlighted on validation error

查看:164
本文介绍了PrimeFaces输入组件在验证错误时未突出显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Seam 2.3.1 Final.而且我在表单上添加了自定义EmailValidation.

I am using Seam 2.3.1 Final. And I have added the Custom EmailValidation on my form.

@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>

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

It works, when I type invalid email, it gives error message text. However, input fields does not switch error-state mode. There is no redline border of input anymore.

推荐答案

您还需要显式覆盖ajax-update中的输入.一种方法是添加表示当前表单的@form.

You need to explicitly cover the inputs in ajax-update as well. One way is adding @form which represents the current form.

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

或通过其显式ID.

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

另一种方法是使用

Another way is using a PFS/jQuery selector to reference only the inputs.

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

一种完全不同的方法是使用OmniFaces <o:highlight> 将PrimeFaces自己的样式表放置在关联的输入(和标签),因此您无需担心显式地对其进行Ajax更新.

A completely different way is using OmniFaces <o:highlight> to put PrimeFaces own style sheet on the associated inputs (and labels) so that you never need to worry about explicitly ajax-updating them.

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

这篇关于PrimeFaces输入组件在验证错误时未突出显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆