p:dialog 验证失败时不隐藏
p:dialog not hidden on validation failure
我无法关闭 PrimeFaces 对话框时遇到问题。输入字段 'username' 为必填项:
<p:outputLabel for="username" value="Username: "/>
<p:inputText id="username"
value="#{employeeController.employee.username}" required="true"/>
我使用以下代码来防止对话框在字段为空时关闭:
<p:commandButton value="Save" action="{employeeController.doSaveEmployee()}"
oncomplete="if (args && !args.validationFailed) PF('employeeAddDialog').hide()"
update=":employeeForm"/>
但是无论 'username' 是否被填充,每当我点击 'Save' 时对话框仍然关闭。当我关闭对话框后再次打开对话框时会显示错误消息,所以我知道输入验证有效。
编辑:
<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 && !args.validationFailed) PF('employeeAddDialog').hide()" update=":employeeForm" />
<p:commandButton value="Abort" oncomplete="PF('employeeAddDialog').hide()" />
</h:panelGrid>
</p:outputPanel>
</p:dialog>
这里还有一些代码。还有什么可能是这个问题的原因?
employeeForm
只是一个 dataTable
,它列出了具有相应属性的员工。
编辑 2:
EmployeeController
中的这段代码会产生所需的行为,但前提是我从 'Save'-button
中删除 update=":employeeForm"
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"
让事情按预期进行。
我建议在成功保存操作后在 employeeController.doSaveEmployee()
中进行关闭操作
RequestContext context = RequestContext.getCurrentInstance();
context.execute("PF('myDialogVar').hide();");
不确定您为什么使用 oncomplete="args & amp; & amp; ..."。我从来没有用过它并且效果很好。
oncomplete="if (args && !args.validationFailed) PF('employeeAddDialog').hide()"
我这样使用它:
oncomplete="if (!args.validationFailed) PF('employeeAddDialog').hide()"
这是一道题错了吗?
我无法关闭 PrimeFaces 对话框时遇到问题。输入字段 'username' 为必填项:
<p:outputLabel for="username" value="Username: "/>
<p:inputText id="username"
value="#{employeeController.employee.username}" required="true"/>
我使用以下代码来防止对话框在字段为空时关闭:
<p:commandButton value="Save" action="{employeeController.doSaveEmployee()}"
oncomplete="if (args && !args.validationFailed) PF('employeeAddDialog').hide()"
update=":employeeForm"/>
但是无论 'username' 是否被填充,每当我点击 'Save' 时对话框仍然关闭。当我关闭对话框后再次打开对话框时会显示错误消息,所以我知道输入验证有效。
编辑:
<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 && !args.validationFailed) PF('employeeAddDialog').hide()" update=":employeeForm" />
<p:commandButton value="Abort" oncomplete="PF('employeeAddDialog').hide()" />
</h:panelGrid>
</p:outputPanel>
</p:dialog>
这里还有一些代码。还有什么可能是这个问题的原因?
employeeForm
只是一个 dataTable
,它列出了具有相应属性的员工。
编辑 2:
EmployeeController
中的这段代码会产生所需的行为,但前提是我从 'Save'-button
update=":employeeForm"
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"
让事情按预期进行。
我建议在成功保存操作后在 employeeController.doSaveEmployee()
中进行关闭操作
RequestContext context = RequestContext.getCurrentInstance();
context.execute("PF('myDialogVar').hide();");
不确定您为什么使用 oncomplete="args & amp; & amp; ..."。我从来没有用过它并且效果很好。
oncomplete="if (args && !args.validationFailed) PF('employeeAddDialog').hide()"
我这样使用它:
oncomplete="if (!args.validationFailed) PF('employeeAddDialog').hide()"
这是一道题错了吗?