0

I am using Jquery smartwizard link, I need to stop the validation when user click on "Previous" button in any step(By default in 1st step Previous button is disabled) except 1st step.

Here is my javascript code

$(document).ready(function () {
      // Smart Wizard       
      $('#wizard').smartWizard({
          transitionEffect: 'fade',
          onLeaveStep: leaveAStepCallback,
          onFinish: onFinishCallback,
          enableFinishButton: false
      });

      function leaveAStepCallback(obj) {
          var step_num = obj.attr('rel');
          return validateSteps(step_num);
      }

      function onFinishCallback() {
          if (validateAllSteps()) {
              $('form').submit();
          }
      }
  });

  function validateAllSteps() {
      var isStepValid = true;
      if (validateStep1() == false) {
          isStepValid = false;
          $('#wizard').smartWizard('setError', {
              stepnum: 1,
              iserror: true
          });
      } else {
          $('#wizard').smartWizard('setError', {
              stepnum: 1,
              iserror: false
          });
      }
      if (validateStep2() == false) {
          isStepValid = false;
          $('#wizard').smartWizard('setError', {
              stepnum: 2,
              iserror: true
          });
      } else {
          $('#wizard').smartWizard('setError', {
              stepnum: 2,
              iserror: false
          });
      }
      if (validateStep3() == false) {
          isStepValid = false;
          $('#wizard').smartWizard('setError', {
              stepnum: 3,
              iserror: true
          });
      } else {
          $('#wizard').smartWizard('setError', {
              stepnum: 3,
              iserror: false
          });
      }
      if (!isStepValid) {
          $('#wizard').smartWizard('showMessage', 'Please correct the errors in the steps and continue');
      }
      return isStepValid;
  }

  function validateSteps(step) {
      var isStepValid = true;
      // validate step 1
      if (step == 1) {
          if (validateStep1() == false) {
              isStepValid = false;
              $('#wizard').smartWizard('showMessage', 'Please correct the errors in step' + step + ' and click next.');
              $('#wizard').smartWizard('setError', {
                  stepnum: step,
                  iserror: true
              });
          } else {
              $('#wizard').smartWizard('hideMessage');
              $('#wizard').smartWizard('setError', {
                  stepnum: step,
                  iserror: false
              });
          }
      }
      // validate step2
      if (step == 2) {
          if (validateStep2() == false) {
              isStepValid = false;
              $('#wizard').smartWizard('showMessage', 'Please correct the errors in step' + step + ' and click next.');
              $('#wizard').smartWizard('setError', {
                  stepnum: step,
                  iserror: true
              });
          } else {
              $('#wizard').smartWizard('hideMessage');
              $('#wizard').smartWizard('setError', {
                  stepnum: step,
                  iserror: false
              });
          }
      }
      // validate step3
      if (step == 3) {
          if (validateStep3() == false) {
              isStepValid = false;
              $('#wizard').smartWizard('showMessage', 'Please correct the errors in step' + step + ' and click next.');
              $('#wizard').smartWizard('setError', {
                  stepnum: step,
                  iserror: true
              });
          } else {
              $('#wizard').smartWizard('hideMessage');
              $('#wizard').smartWizard('setError', {
                  stepnum: step,
                  iserror: false
              });
          }
      }
      return isStepValid;
  }

  function validateStep1() {
      //Validation code here
  }

  function validateStep2() {
      //Validation code here
  }

  function validateStep3() {
      //Validation code here
  }
Alex
  • 8,461
  • 6
  • 37
  • 49
user3635258
  • 51
  • 1
  • 2
  • 5
  • check this link http://stackoverflow.com/questions/9925255/skip-validation-when-going-backwards-in-smartwizard – Suyog Nov 17 '15 at 10:01

2 Answers2

0

...A very reasonable request. Try this...

  function leaveAStepCallback(anchor, context) {
      //var step_num = obj.attr('rel');
      if(context.toStep > context.fromStep)
          return validateSteps(fromStep);
      else
          return true;
  }

The docs suggest you can get fromStep and toStep from the second parameter as shown. I hope this is the case! If not, hunt around for it. It must be there somewhere.

bbsimonbb
  • 27,056
  • 15
  • 80
  • 110
0

You can check for both fromStep and toStep:

function nextStep(smartWizard, steps, context){
                if(steps.fromStep == 1 && steps.toStep == 2){
                    // Do validations
                }
}

I used the following wizard:

$('#wizard').smartWizard({
                    onLeaveStep: nextStep // triggers when leaving a step
            });
Simon Baars
  • 1,877
  • 21
  • 38