7

In my JSF application I am trying to put some conditions in my Command button update attribute so that on validation failure/Success I can control the components to update.

With reference to PrimeFaces: conditional update on validation I tried to to use

<p:commandLink process="@form" listener="#{foo}" 
  update="somethingElse" oncomplete="if (!args.validationFailed) $("#link").click();">
<p:commandLink style="display:none" id="link"
  update="something">

But I don't know why this approach is not working for me.. click event of that particular component is not working for me.. I tried to execute the accepted answer for the same question , but I could not understand how that solve the purpose...

My purpose is to update conditionally depending on the result of the validation success or failure... Support I have 3 components X Y Z.. I am trying to do something like this..

<p:commandLink process="@form" listener="#{foo}" 
      update="if(Validation fail)updateSomething[e.g X and Y] **else** update somethingelse[e.g X  Y and Z]">

To be more precise if the validations fails then I want to update X and Y component only if validations succeed than I want to update X Y & Z components..

Kindly help me out.. Thanks in advance.

Community
  • 1
  • 1
Dhruv
  • 10,291
  • 18
  • 77
  • 126

2 Answers2

13

The first problem is that the EL syntax which you've there is invalid. You're closing the attribute value too soon by using " instead of ' in the JavaScript code. Either escape nested quotes with \" or just use ' instead.

<p:commandLink process="@form" listener="#{foo}" update="somethingElse" 
    oncomplete="if (!args.validationFailed) $('#link').click();">

The second possible problem is that the client ID of the <p:commandLink id="link"> is not "link" at all. It's by default prepended with the ID of the parent <h:form> component. The client ID is the JSF-generated HTML element ID. Open the page in webbrowser, rightclick and View Source to see it yourself. It'll look something like id="formId:link". You need to use exactly that ID in the jQuery selector.

<p:commandLink process="@form" listener="#{foo}" update="somethingElse" 
    oncomplete="if (!args.validationFailed) $('#formId\\:link').click();">

See also:


An alternative is to perform the concrete job in the method behind listener="#{foo}" instead.

if (!facesContext.isValidationFailed()) {
    RequestContext.getCurrentInstance().update("somethingElse");
}
Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • I will prefer the second approach rather than jquery approach... yes I can use above code to update from java code but than How I will stop update than I have specified on the .xhtml.. Is there are way to apply some condition on xhtml side..?? – Dhruv Jun 15 '12 at 16:46
  • @balausC and also can you explain the approach using that you have explained in above mentioned link in the question – Dhruv Jun 15 '12 at 16:53
  • If the sole purpose is displaying faces messages, just use the same approach as in the answer in your link. Replace `oncomplete="if (!args.validationFailed) $('#link').click();"` by `update="messages"`. – BalusC Jun 15 '12 at 17:15
  • My edit to this answer was rejected - don't exactly know why though - however in code example, the code can not compile, because the method isn't called `validationFailed()`, but `isValidationFailed()`. `validationFailed()` returns `void` and not a `boolean`. – Darwind Apr 18 '13 at 08:24
0

I tried the marked answer but it didn't solve my issue since args does not contain parameter called "validationFailed" however I found the way of adding this parameter from the controller.

 import org.primefaces.context.RequestContext;

 RequestContext requestContext = RequestContext.getCurrentInstance();
 requestContext.addCallbackParam("validationError", true);
 JSFUtils.displayErrorMessage("My message is here!");

& in the xhtml I added the following command link

 <p:commandLink ajax="true" action="#{myController.myMethod}"
 oncomplete="if(args.validationError) alert('Error!');"/>
  • Perhaps you're using a jurassic version of PrimeFaces? IIRC it was in 3.x already. – BalusC Apr 14 '16 at 08:10
  • In fact I am using primefaces 4.0 but still I am not able to use the solution without adding the parameter myself using the primefaces RequestContext. – Yasser Gawish Apr 14 '16 at 13:24