0

I'm tying to build a wizard on my website. Like every wizard, it has steps. For every step you can go either forward or backwards to previous step. I'm handling this navigation among steps with ajax. Every step is defined on a div. Each step has the same class, and a unique id which indicates the step. So, for navigating I defined the following js function, On which the parameter is the id of the step to show.

function navigateStepWizard(id) {
        //hide all steps
        $('.wizard-step').hide();
        //show proper step
        $(id).fadeIn("fast");
        return false;
}

Now, the problem I'm facing is that on one of this steps I have several fields that must be validated before going to next step. I've defined validators (Faces Validator Clases) for each field on a form which are being executed when trying to go to next step, returning appropriate Ok/Error messages. My problem is that when this fields are not correct, the button does go to next step. First I've tried to use oncomplete which always executed the js function, so I've changes to onsuccess thinking that when a validation is not ok, the ajax callback wont execute the onsuccess js function, but it does... here's my button code

<p:commandLink id="go-to-step-X" 
               actionListener="#{myBean.submitWizardInfo}" 
               onsuccess="navigateStepWizard('#wizard-step-X');" 
               process="@form, @([id$=wizzard])" 
               update="@form, @([id$=wizzard])">
    Go to next step...                                    
</p:commandLink>

Is there a way to do this avoiding the onsuccess AJAX call when a field is not ok? Thanks for your time!

P.D: I'm using JSF 2.0 & PrimeFaces 5.0.

Tiny
  • 27,221
  • 105
  • 339
  • 599
Rodrigo Martinez
  • 913
  • 3
  • 13
  • 29
  • 1
    Just do `onsuccess="if(args && !args.validationFailed) {navigateStepWizard('#wizard-step-X');}"` instead of taking tedious ways. – Tiny Jan 13 '15 at 20:36

1 Answers1

0

In order to hit the onSuccess call back the server must respond with a status code in the 200 range. If there is something wrong with the validation make sure the response code is in the 400 range. This way the onSuccess function will not be called.

Rupert
  • 1,629
  • 11
  • 23
  • Thanks for the hint! I will force this on validation methods. Is there, by any chance, a good tutorial or guide on how to do this on JSF ? – Rodrigo Martinez Jan 13 '15 at 16:49
  • And this stack overflow answer: http://stackoverflow.com/questions/11428686/how-to-set-http-status-code-with-jsf – Rupert Jan 13 '15 at 16:51
  • Whilst this answer answers the question the OP actually asked (skipping `onsuccess`), this answer actually only makes things worse. The ajax response wouldn't be processed at all. No partial updates would take place. No validation messages would show up. This is absolutely not what the OP intented. This is just a classic case of an [XY-problem](http://meta.stackexchange.com/questions/66377/what-is-the-xy-problem). Once you fix the answer to propose the right apporach for what the OP actually needed, I'll remove the downvote. – BalusC Jan 13 '15 at 22:28
  • I've implemented this suggestion and, as @BalusC just posted, this didn't worked. Actually it avoids the onsuccess call, but then validation messages are not shown. Tried out a different approach, I've set a flag on my backing bean which only set true if validations were ok (on an actionListener) and wizard should go to next step. Additional to this I've define an if on onsuccess that checks on this flag to execute the js function that hides and shows propper step. Rarely when debuuging the actionListener executes and flag is set to true but then the onsuccess js fun doesn't execute – Rodrigo Martinez Jan 14 '15 at 12:16