如何让验证取决于按下的按钮? [英] How to let validation depend on the pressed button?

查看:71
本文介绍了如何让验证取决于按下的按钮?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经创建了表单,并且想在创建新表时在表上显示以前的现有项.我想在表单填写时显示匹配的项目.但是,当我尝试在未完成表单的情况下过滤列表时,将显示验证消息,并且该表没有得到更新.

I have created form and I want to show previous existing items on a table while a new one is creating. I'd like to show matching items as form is filling up. But when I try to filter the list without having the form completed, the validation messages appear and the table doesn't get updated.

不知道是否可能,但是我想做这样的事情:

Don't know if it's possible, but what I want to do something like this:

<h:form id="form">

    <h:outputText value="Name: "/>
    <p:inputText value="#{itemsBean.name}" id="name" required="true"/>
    <br/>
    <h:outputText value="Description: "/>
    <p:inputText value="#{itemsBean.description}" id="description" required="true"/>

    <p:commandButton value="Save" update="form" actionListener="#{itemsBean.save}"/> //validate and save
    <p:commandButton value="Filter" update="form" actionListener="#{itemsBean.updateItemsList}"/> //don't validate, and update the table.

    <p:dataTable id="list" value="#{itemsBean.itemsList}" var="item">     
        <p:column>
            <h:outputText value="#{item.name}"/>
        </p:column>
        <p:column>
            <h:outputText value="#{item.description}"/>
        </p:column>
    </p:dataTable>

</h:form>

我是JSF的新手.

推荐答案

我了解您要基于name输入字段进行过滤. <p:commandButton>默认发送ajax请求,并具有process属性,您可以在其中指定要在提交期间处理的组件.在特定情况下,您应该仅处理 输入域和当前按钮(以便调用其操作).

I understand that you want to filter based on the name input field. The <p:commandButton> sends by default an ajax request and has a process attribute wherein you can specify which components you'd like to process during the submit. In your particular case, you should then process only the name input field and the current button (so that its action will be invoked).

<p:commandButton process="@this name" ... />

process属性可以采用空格分隔的组件(相对)客户端ID的集合,其中@this是指当前组件.如果<p:commandButton>@form(涵盖当前表单的所有输入字段和按下的按钮)的默认值,这就是为什么在您初次尝试时都对它们进行了验证的原因.在上面的示例中,所有其他输入字段都不会被处理(因此也不会得到验证).

The process attribute can take a space separated collection of (relative) client IDs of the components, wherein @this refers to the current component. It defaults in case of <p:commandButton> to @form (which covers all input fields of the current form and the pressed button), that's why they were all been validated in your initial attempt. In the above example, all other input fields won't be processed (and thus also not validated).

但是,如果您打算在每次按下相关按钮时跳过对 all 字段的required验证,以便最终可以处理多个字段,而不必一定要 all 填写,那么您需要将required="true"设置为条件控件,以检查是否按下了按钮.例如,仅当按下保存按钮时,才让它评估true:

If you however intend to skip the required validation for all fields whenever the button in question is been pressed, so that you can eventually process multiple fields which doesn't necessarily need to be all filled in, then you need to make the required="true" a conditional instead which checks if the button is been pressed or not. For example, let it evaluate true only when the save button has been pressed:

<p:inputText ... required="#{not empty param[save.clientId]}" />
...
<p:inputText ... required="#{not empty param[save.clientId]}" />
...
<p:commandButton binding="#{save}" value="Save" ... />

这样,当按下其他按钮时,它将不会被验证为required="true".上面示例中的技巧是,已按下按钮的名称(本质上是客户端ID)作为请求参数发送,您可以在请求参数映射中检查它的存在.

This way it won't be validated as required="true" when a different button is pressed. The trick in the above example is that the name of the pressed button (which is essentially the client ID) is been sent as request parameter and that you could just check its presence in the request parameter map.

这篇关于如何让验证取决于按下的按钮?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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