繁体   English   中英

在键入事件时跳过对Primefaces Inputtext的验证,但在提交时进行验证

[英]Skip validation of Primefaces Inputtext on keyup event but validate on submit

我的InputText有两个要求:

  1. p:inputText的值应立即通过keyup-event在h:outputText中显示在屏幕上
  2. 该值在数据库中应该是唯一的

我正在使用带有Glassfish 4和Java 7的Primefaces 4.0,JSF 2.2

我的代码目前看起来像这样

Example.xhtml

<h:form>
    <p:inputText id="value" value="#{myBean.value}" >
        <p:ajax event="keyup" update="example" process="@this" />
        <f:validator binding="#{uniqueValueValidator}" />
    </p:inputText>
    <h:outputText id="example" value="#{myBean.value}">
    <p:commandButton value="Save" action="#{myBean.saveValue}"/>
</form>

MyBean.java

@Named
@RequestScoped
public class MyBean {

    @Inject
    private DBService service;

    private String value;
    //getter, setter

    public String saveValue() {
        service.saveValue(value);
        return "showall";
    }
}

UniqueValueValidator.java

@Named
public class UniqueValueValidator implements Validator {

    @Inject 
    private DBService service;

    @Override
    public void validate(FacesContext context, UIComponent component, Object value)
        throws ValidatorException {

        if(service.isValueNotUnique(value.toString()) {
              // throw ValidatorException 
        }
    }
}

现在我的问题是,在每个keyup事件中,该值都会得到验证,并会调用数据库。 但是我只想在提交表单时验证值。

我的第一个解决方案是将验证移至saveValue方法。

public String saveValue() {
    if(service.isValueNotUnique(value) {
        // add a FacesMessage
        return null;
    } else {
         service.saveValue(value);
         return "showall";
    }
}

但是在这里,我认为将验证代码和逻辑代码混合在一种方法中并不是一种好习惯。

所以我希望你对我有更好的解决方案;)

问题出在inputText组件内的ajax标记中:

<p:ajax event="keyup" update="example" process="@this" />

这意味着您将在每个keyup事件上提交组件。 因此,每次都会调用验证程序。

一种可能的解决方法是,按照用于验证多个组件的相同技术,将验证器移动到另一个组件:

在facelets页面中,添加一个使用uniqueValueValidator

<h:form id="formId" >
    <p:inputText id="value" value="#{myBean.value}" >
        <p:ajax event="keyup" update="example" process="@this" />
    </p:inputText>
    <h:inputHidden id="hidden">
        <f:validator validatorId="uniqueValueValidator" />
    </h:inputHidden>
    <p:message for="hidden" />
    <p:commandButton value="Save" action="#{myBean.saveValue}" process="@form" update="@form"/>
</h:form>

UniqueValueValidator

@Named
@FacesValidator("uniqueValueValidator")
public class UniqueValueValidator implements Validator {

    @Inject
    private DBService service;

    @Override
    public void validate(FacesContext context, UIComponent component, Object obj) {

        Object inputValue = ((UIInput) context.getViewRoot().findComponent("formId:value")).getSubmittedValue();

        if(service.isValueNotUnique((String) inputValue) {
            // throw ValidatorException 
        }
    }

}

}

像您一样,这种方法意味着在每个keyup事件中输入内容的往返客户端-服务器-客户端。 如果没有必要,您可以使用javascript方法来避免这种情况,如其他答案中所述。

链接

您可以在p:inputText中摆脱ajax请求,而只需使用jquery将值从输入复制到inputtext,如下所示:

<h:form id="myForm">
    <p:inputText id="value" value="#{myBean.value}" onkeyup="$("#myForm\\:example").html($(this).val()">
        <f:validator binding="#{uniqueValueValidator}" />
    </p:inputText>
    <h:outputText id="example" value="#{myBean.value}">
    <p:commandButton value="Save" action="#{myBean.saveValue}"/>
</form>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM