如何让验证取决于按下的按钮?

发布于 2024-12-19 17:49:02 字数 1066 浏览 2 评论 0原文

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

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

<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 很陌生。

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>

I'm very new to JSF.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

和我恋爱吧 2024-12-26 17:49:02

我了解您想要根据 name 输入字段进行过滤。 默认发送 ajax 请求,并具有 process 属性,您可以在其中指定要在提交期间处理哪些组件。在您的特定情况下,您应该处理名称输入字段和当前按钮(以便调用其操作)。

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

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

但是,如果您打算在按下相关按钮时跳过对所有字段的必需验证,以便您最终可以处理不一定需要的多个字段如果全部填写完毕,那么您需要将required="true"设为条件条件,而不是检查按钮是否被按下。例如,让它仅在按下保存按钮时评估 true

<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)作为请求参数发送,您只需检查它在请求参数映射中的存在即可。

另请参阅:

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" ... />

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).

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" ... />

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.

See also:

野生奥特曼 2024-12-26 17:49:02

我已经使用非 ajax 提交对此进行了测试:

<p:inputText ... required="#{not empty param.includeInSave1}" />
...
<p:inputText ... required="true" />
...
<p:commandButton value="Save1" ajax="false">
  <f:param name="includeInSave1" value="true" />
</p:commandButton>

<p:commandButton value="Save2" ajax="false" />

第一个输入仅需要在 Save1 按钮提交时进行验证。

I Have tested this with non-ajax submits:

<p:inputText ... required="#{not empty param.includeInSave1}" />
...
<p:inputText ... required="true" />
...
<p:commandButton value="Save1" ajax="false">
  <f:param name="includeInSave1" value="true" />
</p:commandButton>

<p:commandButton value="Save2" ajax="false" />

The first input is required validated only on Save1 button submit.

乖乖公主 2024-12-26 17:49:02

除了 BalusC 答案(非常有用且完整)之外,我想补充一点,当您使用 时,它将验证(必需的自定义验证)< code>命令按钮所在的位置,因此当您需要使用多个命令按钮时,您可以考虑使用不同的 分配不同的职责,以避免命令按钮的提交操作中出现意外行为。
BalusC 答案中对此进行了很好的解释: Multiple h:form in a JSF Page

如果您的表单有验证,而您没有更新 或者您不显示消息,您可能会头痛 没有触发您的操作,但可能是尚未显示的验证问题。

Additionally to the BalusC answer (very useful and complete) I want to add that when you use a <h:commandButton /> it will validate (required, custom validations) all the fields in the <h:form /> where the command button is located, therefore when you need to use more than one command button you could consider that it is a good practice to use different <h:form /> to different responsibilities to avoid unexpected behavior in submit actions of the command buttons.
It is well explained in a BalusC answer: Multiple h:form in a JSF Page

If your form has validations and you do not update the <h:form /> or you do not show messages, you could get a headache thinking that the <h:commandButton /> is not firing your action, but likely is a validation problem that has not been shown.

落花浅忆 2024-12-26 17:49:02

像这样更改您的过滤器命令按钮以忽略验证:

<p:commandButton value="Filter" update="list" actionListener="#{itemsBean.updateItemsList}" process="@this"/>

编辑:

SO 上的相关帖子,我认为这也能解决您的问题

JSF 2.0:如何跳过 JSR-303 bean 验证?

Change your filter commandbutton like this to ignore validation:

<p:commandButton value="Filter" update="list" actionListener="#{itemsBean.updateItemsList}" process="@this"/>

EDIT:

The related post on SO, I think this will solve your issue too

JSF 2.0: How to skip JSR-303 bean validation?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文