我遇到无法关闭 PrimeFaces 对话框的问题。输入字段“用户名”是必需的:
<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"/>
但是,无论“用户名”是否填写,每当我单击“保存”时,对话框仍然会关闭。当我关闭后再次打开对话框时,会显示错误消息,因此我知道输入验证有效。
编辑:
<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
列出员工及其相应的属性。
EDIT2:
这段代码在EmployeeController
产生想要的行为,但前提是我删除update=":employeeForm"
从“保存”按钮
public void doSaveEmployee() {
employee = employeeService.saveEmployee(employee);
employee = null;
initNewEmployee();
initList();
RequestContext context = RequestContext.getCurrentInstance();
context.execute("PF('employeeAddDialog').hide();");
}
最终编辑:
我的对话框正在关闭,因为我更新了整个表单。改变update=":employeeForm"
to update=":employeeForm:employeeTable"
使事情按预期进行。