p:dialog not hidden on validation failure

拈花ヽ惹草 提交于 2021-02-08 06:34:24

问题


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.

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 &amp;&amp; !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 &amp;&amp; !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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!