7

I am using the SmartWizard 2.0 (link) and I need to stop the validation from kicking in when the users hits the "Prev" button or in any way moves backward in the form.

Currently I am using

// Triggers whenever you change page/tab in wizard    
function leaveStep(obj) {        
    $("form").validate();
    if ($("form").valid())
        return true;

    return false;
}

I know that I can use

var currentStep = obj.attr('rel'); // get the current step number

to find the currentStep, but I need to know which way the user is navigating - so I need to know the "nextStep". Don't know if this is possible.

Mat
  • 202,337
  • 40
  • 393
  • 406
Stian
  • 1,261
  • 2
  • 19
  • 38

3 Answers3

13

In case anyone happens upon this later (as somebody just did, and emailed me due to their confusion): There is a newer version available on github.

Among other things, it provides your callback with "fromStep" and "toStep" values in an object.

For example:

$('#wizard').smartWizard({
    onLeaveStep:function(obj, context) {
        if (context.fromStep > context.toStep) {
            // Going backward
        } else {
            // Going forward
        }
    }
});
Mark
  • 131
  • 1
  • 2
9

When onLeaveStep is triggered could you not use the obj to determine the direction? Then you would only validate when moving to the next step?

Updated:

After looking at the source code, there is no way that I can see to figure out the direction. Patching in the ability is pretty easy however. In jquery.smartWizard-2.0.js change line 186 from

if(!options.onLeaveStep.call(this,$(curStep))){

to

if(!options.onLeaveStep.call(this,$(curStep),$(selStep))){

This now gives you access to the selected step anchor, and thus the selected step index. To determine the direction in your onLeaveStep handler simple do the following:

// Triggers whenever you change page/tab in wizard    
function leaveStep(from, to) {
    var fromStepIdx = from.attr( "rel" );
    var toStepIdx = to.attr( "rel" );

    if ( toStepIdx < fromStepIdx )
    {
        return true;
    }

    $("form").validate();
    if ($("form").valid())
        return true;

    return false;
}
Marc Gagne
  • 819
  • 5
  • 7
  • That would be what I am looking for - but I can not figure out how to determine the direction as the obj, as far as I know, only tell me that I am on step 2, not where I am going to next. If you know anything more about that it would sure help out! – Stian Apr 07 '12 at 22:06
  • I've updated my answer with a workable solution. If this solves your problem then I recommend creating and submitting a patch here: http://sourceforge.net/projects/smartwizard/ – Marc Gagne Apr 08 '12 at 00:31
7

Mark's answer is correct, you can use the context.fromText and context.toStep to detect direction, but I found that without return true;, smartWizard may not validate the transition (going from step 1 to 2 is allowed, not step 1 to 3, etc.) and allow it to occur.

$('#wizard').smartWizard({
onLeaveStep:function(obj, context) {
    if (context.fromStep > context.toStep) {
        // Going backward
    } else {
        // Going forward
    }
    return true;
}
});
adamup
  • 1,508
  • 19
  • 29