Search code examples
validationjsfjsf-2primefacesjava-ee-6

JSF: Update conditionally on Success or failure of validations


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.


Solution

  • 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");
    }