Question

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?

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top