7

I have a JSF 2.0 page that uses Primefaces p:dialog component. The user can enter information in the dialog and click save/cancel. If they have a validation error, it is shown on the dialog. When they close or cancel the dialog, I want any p:message components to be "wiped out", i.e. so that if the user then opens p the dialog again, they wont see old validation messages.

Use case is: user opens dialog, doesnt fill all required fields, clicks Save, required field error messages are shown in the dialog. User clicks cancel which closes the dialog. User then clicks the button to open up the dialog again. I dont want them to see the previous validation messages (which currently they can).

BestPractices
  • 12,738
  • 29
  • 96
  • 140

1 Answers1

8

Ajax-update the dialog component before opening it.

E.g.

<p:commandButton ... update=":dialog" oncomplete="dialog.open()" />

It will not only clear messages, but also input field values (or at least preserve them with right defaults).

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • What if I want to also clear the input field values as well? (this doesnt seem to do it) – BestPractices Apr 18 '12 at 15:54
  • The action method of the above `` should prepare the dialog's content by e.g. setting the currently selected entity or creating a new entity. – BalusC Apr 18 '12 at 15:56
  • what is the difference between specifying ":dialog" versus "dialog" if the id of the dialog is "dialog"? For me, my form has an id of "form", so the client id for my dialog is "form:dialog". Does :dialog mean "update all dialogs?" – BestPractices Apr 18 '12 at 20:49
  • If the dialog is in the same naming container as the button, then just use `update="dialog"`. But the consensus is to put the dialog outside any form and give it its own form. Otherwise you've got to take into account that you don't "accidently" submit (and thus process/convert/validate) all other inputs *outside* the dialog while submitting the dialog. – BalusC Apr 18 '12 at 20:51
  • is there a resource out there for Primefaces best practices? i.e. how did you know this was the consensus? (primefaces forum, etc?) – BestPractices Apr 19 '12 at 14:40
  • 1
    Yes. Just keep reading questions & answers on Stack Overflow and maybe PrimeFaces forum (I don't). You'll learn a lot of things not directly related to your current situation but which you can apply to your current or future projects. – BalusC Apr 19 '12 at 14:48
  • Should the form go outside the component or within the component? e.g. or – BestPractices Apr 19 '12 at 15:10
  • 1
    Inside. Another reason is that the `` can be relocated to end of body by `modal="true"`. This way it would not be inside a form anymore in the resulting HTML DOM tree. The dialog should have its own form. – BalusC Apr 19 '12 at 15:16
  • 1
    @BalusC, in my case the primefaces valdidation message was wiped properly, but the jsf validation (just red borders on inputs) continues when dialog is showed. Do you have idea? Thanks. – Rafael Orágio Jul 26 '13 at 20:33
  • 1
    @Rafael: it will indeed not clear invalidated state, head to http://stackoverflow.com/questions/12619228/reset-input-fields-without-executing-validation/12619890#12619890 and http://stackoverflow.com/questions/6642242/how-can-i-populate-a-text-field-using-primefaces-ajax-after-validation-errors-oc/6845800#6845800 then. – BalusC Jul 26 '13 at 20:36