0

I have this commandbutton:

<p:commandButton action="#{reportBB.createReport}" ajax="false" id="createReport" onclick="form.target='_blank'" value="#{msg['create.report']}" />

This open a new page and load a pdf report. It always open the new page and then validate the required fields. I need to validate that all required fields have been filled at first and then open the new page.

Is this possible? What can I do?

John Alexander Betts
  • 4,718
  • 8
  • 47
  • 72
  • You need 2 `UICommand`s: 1 that just triggers the field validations. If all validations passes, this will call the second `UICommand` with `target="blank"` that will execute your real action `"#{reportBB.createReport}"` – Luiggi Mendoza Oct 11 '13 at 20:25
  • @LuiggiMendoza I really don't kwow how to do this. Could you show an example please? – John Alexander Betts Oct 11 '13 at 20:42
  • @JohnB - Do you intend to halt the pdf loading on validation failure? If you do, set `immediate="true"` on the fields desired – kolossus Oct 12 '13 at 16:44
  • No, I need to validate the required fields at first and if the required files has been filled then open the new window with the pdf file – John Alexander Betts Oct 12 '13 at 19:55

1 Answers1

1

You'll first need to change the onclick to use oncomplete instead. Then tie it's execution to the validation status of the request variable:

<p:commandButton action="#{reportBB.createReport}" ajax="false" id="createReport" oncomplete="if (!args.validationFailed){form.target='_blank'}" value="#{msg['create.report']}" />

The reason why you'll have to switch to oncomplete is that oncomplete runs after the JSF request has been processed, up to at least the validations/conversions phase. It's in the validations phase that the status of the request is set, and it's based on that status that you'll display the PDF. onclick is too early for the same reasons.

The args object is PF object that carries some useful info. My snippet above interrogates it for the validation status of the request.

I believe that's the core of your question. A few pointers about actually validating what I presume to be multiple fields in your form:

  1. The simplest route is to set immediate="true" on the input components that you're validating. This way, if validation fails, the action of the command button is never called. All you need to ensure is that the command button is in the same form as the input components.

  2. You can use the omnifaces validateAll component to validate multiple fields in one place

  3. A alternative is to disable the command button and ajax-enable it when all the fields in the form have passed validation.

kolossus
  • 20,559
  • 3
  • 52
  • 104
  • I changed the onclick attribute to oncomplete as you suggest but it doesn't open the new window when validation passes. The first point you mentioned (immediate="true") fails because my input components are dependent oneselectmenu and throws a NullPointerException when try to load the information in the other oneselectmenu component. I have tried to do the third point but I can't. I am a newbie in JSF and I don't know how to do it well. I have not tried the second point yet – John Alexander Betts Oct 15 '13 at 13:20
  • I upvoted your answer by your effort. Could you help me please – John Alexander Betts Oct 15 '13 at 13:24
  • @JohnB - Let me understand you correctly, the destination page where the pdf is rendered is where? I mean, are you returning a navigation case in `createReport`? btw, the reason the navigation is not happening is because you're not allowed to conditionally change the form's target *after* the view as been rendered. So `form.target='blank'` is illegal in a commandButton – kolossus Oct 15 '13 at 14:55
  • The destination page is another page (_blank) if the required fields are filled, if not my new page(_blank) should not be opened – John Alexander Betts Oct 15 '13 at 15:12
  • @JohnB - then you don't need to use `onclick` or `oncomplete` at all. Simply change `createReport` method to return a `String` outcome that corresponds to the page you're trying to navigate to. Then set `target="_blank"` in your ``. You've also set your fields to `required="true"` right? – kolossus Oct 15 '13 at 16:42
  • It doesn't work. It opens a new window when the even when the required fields have not been filled – John Alexander Betts Oct 15 '13 at 18:26
  • @JohnB then there's something else seriously wrong with the rest of your code. If validation fails, the action must not be executed - that is standard JSF. Are you sure the `` and the input fields are in the same ``? Post the rest of your view here. – kolossus Oct 15 '13 at 19:28
  • It opens the new page asking for the required fields and the action is not executing, but I really need that the new page only opens when the required fields are filled. In a moment I will put the view. Thanks – John Alexander Betts Oct 15 '13 at 20:03
  • Thanks @kolossus I have selected the third option you suggested – John Alexander Betts Oct 28 '13 at 13:08