在键入事件时跳过对Primefaces Inputtext的验证,但在提交时进行验证 [英] Skip validation of Primefaces Inputtext on keyup event but validate on submit

查看:57
本文介绍了在键入事件时跳过对Primefaces Inputtext的验证,但在提交时进行验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对InputText有两个要求:

I have two requirements for my InputText:

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

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

I'm using Primefaces 4.0, JSF 2.2 with Glassfish 4 and Java 7

此刻我的代码是这样的

Example.xhtml

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

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

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事件中,该值都会被验证并调用数据库.但是我只想在提交表单时验证值.

My problem is now, that on every keyup-event the value is validated and a call to the database is made. But I want to validate the value only when the form is submitted.

我的第一个解决方案是将验证转移到saveValue方法中.

My first solution was to move the validation into the saveValue method.

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

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

But here I think it is not good practice to mix validation code and logic code in one method.

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

So I hope you have a nicer solution for me ;)

推荐答案

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

The issue lies in the ajax tag inside the inputText component:

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

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

This means that you are submitting the component on each keyup event. And the validator will be called each time consequently.

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

A possible workaround is to move the validator to another component, following the same technique used for validating multiple components:

在facelets页面中,添加使用uniqueValueValidator的inputHidden:

In the facelets page add an inputHidden that uses 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方法来避免这种情况.

This approach, like yours, implies a round-trip client-server-client of the inputed content on each keyup event. If this is not necessary, you can avoid it with a javascript approach as explained in the other answer.

链接

  • The BalusC Code: Validator for multiple components
  • jsf validate two fields in one time

这篇关于在键入事件时跳过对Primefaces Inputtext的验证,但在提交时进行验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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