跳过重新提交时的验证,仅适用于表单提交 [英] Skip validation on re-render and apply only on form submit

查看:145
本文介绍了跳过重新提交时的验证,仅适用于表单提交的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个输入字段,用户可以在其中输入一些产品名称.根据输入的名称,应显示该产品的特定详细信息.在输入更改时,我通过当前字段值从数据源获取产品,然后使用AJAX重新呈现产品详细信息.到目前为止,用户只有输入正确的名称,才能看到产品详细信息.

I have an input field where user enters some product name. Depending on an entered name particular details of this product should be shown. On input change I'm getting the product from data source by the current field value and re-render product details using AJAX. So far user can not see product details until he enters correct name.

这是代码示例:

<h:form>
    <h:inputText id="product" value="#{myBean.product}" required="true" converter="productConverter">
        <a4j:ajax event="change" render="product, details"/>
    </h:inputText>

    <rich:message for="product" ajaxRendered="true"/>

    <h:panelGroup id="details">
        <h:outputText rendered="#{not empty myBean.product}" value="#{myBean.product.details}"/>
    </h:panelGroup>

    <a4j:commandButton execute="@form" render="@form" value="Validate" action="validate"/>
</h:form>

转换器:

public class ProductConverter implements Converter {

public Object getAsObject(FacesContext context, UIComponent component, String name) {
    return (name != null && ProductDataSource.projectExist(name)) ? new Product(name) : null;
}

public String getAsString(FacesContext context, UIComponent component, Object value) {
    return (value instanceof Product) ? ((Product) value).getName() : "";
}

该产品为必填字段,因此只有输入正确的名称后才能提交表单.到目前为止,当用户输入错误的名称时,转换器将返回null且验证失败.但是我想要的是仅在表单提交时验证此字段,并在重新呈现输入字段时跳过验证.我尝试在a4j:ajaxh:inputText上同时应用immediate="true",但没有任何帮助.有任何想法吗?

The product is mandatory field, so form can't be submitted until the correct name is entered. So far when user enters incorrect name, converter returns null and validation fails. But what I want is to validate this field only on form submit, and skip validation when re-rendering input field. I tried to apply immediate="true" on both a4j:ajax and h:inputText but nothing helped. Any ideas?

提前谢谢!

(我正在使用JSF 2,Richfaces 4)

(I'm using JSF 2, Richfaces 4)

推荐答案

仅当按下提交按钮时,让required属性评估true.您可以通过在请求参数映射中检查其客户端ID的存在来做到这一点,例如:

Let the required attribute evaluate true only if the submit button is pressed. You can do that by checking the presence of its client ID in the request parameter map like so:

<h:inputText ... required="#{not empty param[button.clientId]}" />
...
<a4j:commandButton binding="#{button}" ... />

(不,您不需要将其绑定到bean属性,代码按原样完整)

这篇关于跳过重新提交时的验证,仅适用于表单提交的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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