使用新值更新验证后,如何重置表单中的输入字段失败 [英] How to reset input fields in a form after validation fail when updating it with new values

查看:86
本文介绍了使用新值更新验证后,如何重置表单中的输入字段失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将JSF 2与PrimeFaces 4.0结合使用.

I'm using JSF 2 with PrimeFaces 4.0.

我有一个带有一些输入和验证的表格.从对话框中选择实体时,某些字段可能会自动填充.当我在提交整个表单(并验证)之前选择实体时,这可以很好地工作.但是,当我在验证后选择一个实体时,该组件的更新将不起作用.

I have a form with some inputs and validation. Some fields may automatically filled when choosing an entity from a dialog. This works fine when I choose the entity before I submit the whole form (and validate). But when I choose an entity after validation the update of the component does not work.

JSF页面:

<h:form id="form">
    <p:inputText id="id" value="#{bean.user.id}" required="true" 
        onclick="PF('usersDialog').show()" />
    <p:inputText id="name" value="#{bean.user.name}" required="true"
        onclick="PF('usersDialog').show()" />
    <p:commandButton value="submit" action="#{bean.submit}" update=":form" />
</h:form>

<p:dialog widgetVar="usersDialog">
    <h:form>
        <p:dataTable value="#{bean.users}" var="user">
            <p:column>
                <p:commandButton value="choose" onclick="PF('usersDialog').hide()"
                    process="@this" action="#{bean.select(user)}" update=":form" />
            </p:column>
            <p:column>
                <h:outputText value="#{user.name}" />
            </p:column>
        </p:dataTable>
    </h:form>
</p:dialog>

我知道它直到JSF生命周期,但是我无法修复它. 那么,从对话框中选择新实体时,验证失败后如何更新表单?

I know its up to the JSF lifecycle, but I'm not able to fix it. So how can I update the form after validation fail when choosing an new entity from dialog?

谢谢.

推荐答案

正是出于这个目的, <p:resetInput>组件(基于 OmniFaces ResetInputAjaxActionListener )

Exactly for this purpose, <p:resetInput> component was introduced (based on OmniFaces ResetInputAjaxActionListener).

将其嵌套在命令按钮中,并将target设置为与命令按钮的update属性相同的客户端ID:

Nest it inside the command button, with a target set to the same client ID as update attribute of command button:

<p:commandButton ... update=":form">
    <p:resetInput target=":form" />
</p:commandButton>

另请参见:

  • 重置输入字段而不执行验证
  • See also:

    • How can I populate a text field using PrimeFaces AJAX after validation errors occur?
    • Reset input fields without executing validation
    • 这篇关于使用新值更新验证后,如何重置表单中的输入字段失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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