问题
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 && !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.
EDIT:
<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>
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.
EDIT2:
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();");
}
FINAL EDIT:
My dialog was closing because I updated the whole form. Changing update=":employeeForm"
to update=":employeeForm:employeeTable"
made things work like intended.
回答1:
i suggest make close operaction in employeeController.doSaveEmployee()
after succes save action
RequestContext context = RequestContext.getCurrentInstance();
context.execute("PF('myDialogVar').hide();");
回答2:
not sure why you are using oncomplete="args & amp; & amp; ...". I have never used that and works great.
oncomplete="if (args && !args.validationFailed) PF('employeeAddDialog').hide()"
I use it like:
oncomplete="if (!args.validationFailed) PF('employeeAddDialog').hide()"
is this a question error?
来源:https://stackoverflow.com/questions/43890627/pdialog-not-hidden-on-validation-failure