p:验证失败时未隐藏对话框 [英] p:dialog not hidden on validation failure

查看:45
本文介绍了p:验证失败时未隐藏对话框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在不关闭PrimeFaces对话框时遇到问题.输入字段用户名"为必填项:

I am having a problem with not closing a PrimeFaces dialog. The input field 'username' is required:

<p:outputLabel for="username" value="Username: "/>
<p:inputText id="username"
    value="#{employeeController.employee.username}" required="true"/>

我使用以下代码来防止如果该字段为空,则对话框关闭:

I used the following code to prevent the dialog from closing if the field is empty:

<p:commandButton value="Save" action="{employeeController.doSaveEmployee()}"
    oncomplete="if (args &amp;&amp; !args.validationFailed) PF('employeeAddDialog').hide()"
    update=":employeeForm"/>

但是,无论是否填写用户名",只要单击保存",对话框仍会关闭.当我关闭后再次打开对话框时,将显示错误消息,因此我知道输入验证有效.

But the dialog still closes whenever I click on 'Save' no matter if 'username' is filled or not. When I open the dialog again after closing the error message is displayed, so I know that the input validation works.

<p:dialog header="Create Employee" id="employeeAddDialog" widgetVar="employeeAddDialog" modal="true" showEffect="fade" hideEffect="fade" resizable="false">
  <p:outputPanel id="employeeDataCreate">
    <h:panelGrid columns="2">
      <p:outputLabel for="username" value="Username: " />
      <p:inputText id="username" value="#{employeeController.employee.username}" required="true" />

      <p:outputLabel for="password" value="Password: " />
      <p:password id="password" value="#{employeeController.employee.password}" />
    </h:panelGrid>
    <h:panelGrid columns="3">
      <p:commandButton value="Save" action="#{employeeController.doSaveEmployee()}" oncomplete="if (args &amp;&amp; !args.validationFailed) PF('employeeAddDialog').hide()" update=":employeeForm" />
      <p:commandButton value="Abort" oncomplete="PF('employeeAddDialog').hide()" />
    </h:panelGrid>
  </p:outputPanel>
</p:dialog>

还有更多代码.还有什么可能导致此问题? employeeForm只是一个dataTable,其中列出了具有相应属性的员工.

Here is some more code. What else could be the cause for this problem? employeeForm is just a dataTable that lists employees with their corresponding attributes.

EmployeeController中的这段代码会产生所需的行为,但前提是我必须从保存"按钮中删除update=":employeeForm"

This code in EmployeeController yields the wanted behavior, but only if I remove update=":employeeForm" from the 'Save'-button

public void doSaveEmployee() {
    employee = employeeService.saveEmployee(employee);
    employee = null;
    initNewEmployee();
    initList();
    RequestContext context = RequestContext.getCurrentInstance();
    context.execute("PF('employeeAddDialog').hide();");
}

最终

由于我更新了整个表单,因此我的对话框关闭了.将update=":employeeForm"更改为update=":employeeForm:employeeTable"可使事情按预期工作.

My dialog was closing because I updated the whole form. Changing update=":employeeForm" to update=":employeeForm:employeeTable" made things work like intended.

推荐答案

我建议在成功保存操作后在employeeController.doSaveEmployee()中进行关闭操作

i suggest make close operaction in employeeController.doSaveEmployee() after succes save action

RequestContext context = RequestContext.getCurrentInstance();
context.execute("PF('myDialogVar').hide();");

这篇关于p:验证失败时未隐藏对话框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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