2012-02-08 14:57:52 8 Comments
Minimal example dialog:
<p:dialog header="Test Dialog"
widgetVar="testDialog">
<h:form>
<p:inputText value="#{mbean.someValue}"/>
<p:commandButton value="Save"
onsuccess="testDialog.hide()"
actionListener="#{mbean.saveMethod}"/>
</h:form>
</p:dialog>
What I want to be able to do is have the mbean.saveMethod somehow prevent the dialog from closing if there was some problem and only output a message through growl. This is a case where a validator won't help because there's no way to tell if someValue is valid until a save is submitted to a back end server. Currently I do this using the visible attribute and point it to a boolean field in mbean. That works but it makes the user interface slower because popping up or down the dialog requires hitting the server.
Related Questions
Sponsored Content
1 Answered Questions
[SOLVED] Keep p:dialog open when validation failed
- 2019-09-25 09:36:44
- Spasitel
- 36 View
- 0 Score
- 1 Answer
- Tags: jsf primefaces dialog
3 Answered Questions
[SOLVED] How can I populate a text field using PrimeFaces AJAX after validation errors occur?
- 2011-07-10 16:21:08
- Erick Martinez
- 34572 View
- 45 Score
- 3 Answer
- Tags: ajax validation jsf primefaces
1 Answered Questions
[SOLVED] p:dialog gets closed on validation error of a submit with ajax="false", how to keep dialog open?
- 2013-04-22 15:21:25
- Sabarish
- 2453 View
- 5 Score
- 1 Answer
- Tags: validation jsf primefaces dialog download
4 Answered Questions
[SOLVED] Keep <p:dialog> open when validation has failed
- 2013-01-14 22:40:17
- Fabio B.
- 9665 View
- 12 Score
- 4 Answer
- Tags: validation jsf jsf-2 primefaces
1 Answered Questions
[SOLVED] Keep p:dialog up when validation failed
- 2015-01-11 15:31:59
- Ricky77719
- 2464 View
- 2 Score
- 1 Answer
- Tags: validation jsf jsf-2 primefaces
1 Answered Questions
[SOLVED] When data fails first validation, all subsequent submissions fail, even if valid data is submitted
- 2014-04-04 15:54:12
- V. Keating
- 153 View
- 0 Score
- 1 Answer
- Tags: javascript validation struts2 dojo modal-dialog
1 Answered Questions
[SOLVED] p:inputtext format number like '999,999'
- 2013-06-11 16:25:17
- ramo102
- 1892 View
- 1 Score
- 1 Answer
- Tags: javascript jquery jsf primefaces mask
1 Answered Questions
[SOLVED] how to use p:growl only for confirmation not validation jsf2 primefaces
- 2013-03-24 12:01:15
- atbegin-but
- 6526 View
- 7 Score
- 1 Answer
- Tags: validation jsf-2 primefaces message growl
1 Answered Questions
[SOLVED] Automatically show validation messages in p:dialog on validation failure
- 2012-09-17 13:44:33
- tomi
- 17618 View
- 7 Score
- 1 Answer
- Tags: validation jsf dialog primefaces
6 comments
@BalusC 2012-02-08 15:02:42
The
onsuccess
runs if ajax request itself was successful (i.e. there's no network error, uncaught exception, etc), not if action method was successfully invoked.Given a
<p:dialog widgetVar="testDialog">
, you could remove theonsuccess
and replace it by PrimeFacesRequestContext#execute()
insidesaveMethod()
:Note:
PF()
was introduced in PrimeFaces 4.0. In older PrimeFaces versions, you needtestDialog.hide()
instead.If you prefer to not clutter the controller with view-specific scripts, you could use
oncomplete
instead which offers anargs
object which has a booleanvalidationFailed
property:The
if (args)
check is necessary because it may be absent when an ajax error has occurred and thus cause a new JS error when you try to getvalidationFailed
from it; the&
instead of&
is mandatory for the reason explained in this answer, refactor if necessary to a JS function which you invoke likeoncomplete="hideDialogOnSuccess(args, testDialog)"
as shown in Keep <p:dialog> open when validation has failed.If there is however no validation error and the action method is successfully triggered, and you would still like to keep the dialog open because of e.g. an exception in the service method call, then you can manually trigger
validationFailed
totrue
from inside backing bean action method by explicitly invokingFacesContext#validationFailed()
. E.g.@JOTN 2012-02-09 01:35:13
Using RequestContext is pretty interesting. I didn't know you could do that.
@Steven Shaw 2013-01-11 01:00:52
The expression in oncomplete is needs to be negated. oncomplete="if (args.validationFailed) ... "
@Kerem Baydoğan 2013-03-11 16:10:06
@BalusC
oncomplete="if(args && !args.validationFailed)
this is my way of doing it. isnull
check in my code unneccessary?@banterCZ 2013-04-19 07:40:42
@BalusC It works except error messages. I have to update="@form", right? But it close the dialog even validation errors.
@BalusC 2013-04-19 10:31:27
@banterCZ: apparently you aren't performing validation using normal JSF validators, but manually in e.g. action methods by manually adding faces messages.
@banterCZ 2013-04-19 10:41:01
@BalusC No, it is just <p:password required="true" />. But I have had bad order of
form
anddialog
@BalusC 2013-04-19 10:42:50
@banterCZ: The dialog must have its own form, yes. Looking in the generated HTML DOM tree should clear that up.
@Mahmoud Saleh 2013-06-27 11:25:07
@BalusC, why using actionlistener instead of action in this case ?
@BalusC 2013-06-27 11:35:58
@Mah: I was just taking over OP's original code. The question wasn't about actionListener vs action, so I kept OP's original code as is. But I agree that this is not the recommended way, for the case you're wondering.
@Jorge Campos 2015-03-30 19:11:27
@BalusC I've with this problem and I solve part of it with the
RequestContext
solution, thank you for that. Is there a way to update a component in the same way? I ask because I want to update one or other component depending if there is an error which I keep the dialog open therefore update the form in dialog or it is ok and I close the dialog and update the globalMessages@BalusC 2015-03-31 05:48:55
@Jorge: explore the methods of
RequestContext
.@Jorge Campos 2015-03-31 11:12:19
@BalusC I've already did it
RequestContext#update
Thank you so much!!@Rafi 2018-11-26 13:58:36
In PrimeFaces 6.2 this code is deprecated, use:
PrimeFaces.current().executeScript("PF('testDialog').hide()");
instead.@soltysh 2013-11-07 11:02:30
I've just googled up this solution. Basically the idea is to use actionListener instead of button's action, and in backing bean you add callback parameter which will be then check in button's oncomplete method. Sample partial code:
JSF first:
Backing bean:
Hope this helps someone :)
@vhunsicker 2015-02-27 11:40:29
Link changed to here.
@makkasi 2016-07-11 15:27:46
The easiest solution is to not have any "widget.hide", neither in onclick, neither in oncomplete. Remove the hide functions and just put
for the dialog tag
@CleitonCardoso 2019-09-05 22:24:26
You made my day after three. Thanks for that!
@Antaeus 2014-05-29 11:43:40
I use this solution:
JSF code:
Backing bean code:
@Luís Soares 2013-04-17 20:01:17
I believe this is the cleanest solution. Doing this you don't need to change your buttons code. This solution overrides the hide function prototype.
This way, you can keep your code like:
@Alonso Dominguez 2012-02-08 16:08:55
Using the
oncomplete
attribute from your command button and really simple script will help you a lot.Your dialog and command button would be something similar to this:
An the script would be something like this: