diff --git a/src/FormWizard.php b/src/FormWizard.php index 8fd7c1f..b5e3a77 100644 --- a/src/FormWizard.php +++ b/src/FormWizard.php @@ -12,20 +12,20 @@ */ namespace buttflattery\formwizard; -use buttflattery\formwizard\assetbundles\bs3\FormWizardAsset as Bs3Assets; -use buttflattery\formwizard\assetbundles\bs4\FormWizardAsset as Bs4Assets; use Yii; -use yii\base\InvalidArgumentException as ArgException; +use yii\web\View; use yii\base\Widget; -use yii\bootstrap4\ActiveForm as BS4ActiveForm; -use yii\bootstrap4\BootstrapAsset as BS4Asset; -use yii\bootstrap\ActiveForm as BS3ActiveForm; -use yii\helpers\ArrayHelper; use yii\helpers\Html; use yii\helpers\Json; use yii\web\JsExpression; -use yii\web\View; +use yii\helpers\ArrayHelper; +use yii\bootstrap4\BootstrapAsset as BS4Asset; +use yii\bootstrap\ActiveForm as BS3ActiveForm; use buttflattery\formwizard\traits\WizardTrait; +use yii\bootstrap4\ActiveForm as BS4ActiveForm; +use yii\base\InvalidArgumentException as ArgException; +use buttflattery\formwizard\assetbundles\bs3\FormWizardAsset as Bs3Assets; +use buttflattery\formwizard\assetbundles\bs4\FormWizardAsset as Bs4Assets; /** * A Yii2 plugin used for creating stepped form or form wizard using @@ -76,8 +76,8 @@ class FormWizard extends Widget private $_tabularEventJs; /** - * Used for adding limit var for the tabular steps to be used in javascript - * + * Used for adding limit var for the tabular steps to be used in javascript + * * @var mixed */ private $_rowLimitJs; @@ -89,7 +89,6 @@ class FormWizard extends Widget */ private $_persistenceEvents; - //options widget /** @@ -374,7 +373,6 @@ class FormWizard extends Widget */ public $classListGroupBadge = 'success'; - /** * ICONS * */ @@ -417,7 +415,7 @@ class FormWizard extends Widget self::THEME_ARROWS => 'Arrows', self::THEME_MATERIAL => 'Material', self::THEME_MATERIAL_V => 'MaterialVerticle', - self::THEME_TAGS => 'Tags' + self::THEME_TAGS => 'Tags', ]; /** @@ -495,7 +493,7 @@ public function getPluginOptions() 'toolbarPosition' => $this->toolbarPosition, 'showNextButton' => false, 'showPreviousButton' => false, - 'toolbarExtraButtons' => $this->toolbarExtraButtons + 'toolbarExtraButtons' => $this->toolbarExtraButtons, ], 'anchorSettings' => [ 'anchorClickable' => false, @@ -503,8 +501,8 @@ public function getPluginOptions() 'markDoneStep' => $this->markDoneStep, 'markAllPreviousStepsAsDone' => $this->markAllPreviousStepsAsDone, 'removeDoneStepOnNavigateBack' => $this->removeDoneStepOnNavigateBack, - 'enableAnchorOnDoneStep' => $this->enableAnchorOnDoneStep - ] + 'enableAnchorOnDoneStep' => $this->enableAnchorOnDoneStep, + ], ]; } @@ -663,8 +661,8 @@ public function createFormWizard() 'type' => self::STEP_TYPE_PREVIEW, 'title' => 'Final Preview', 'description' => 'Final Preview of all Steps', - 'formInfoText' => 'Click any of the steps below to edit them' - ] + 'formInfoText' => 'Click any of the steps below to edit them', + ], ] ); } @@ -761,6 +759,8 @@ public function createBody($index, $formInfoText, $step) //get the step type $stepType = ArrayHelper::getValue($step, 'type', self::STEP_TYPE_DEFAULT); + $isSkippable = ArrayHelper::getValue($step, 'isSkippable', false); + //check if tabular step $isTabularStep = $stepType == self::STEP_TYPE_TABULAR; @@ -775,7 +775,8 @@ public function createBody($index, $formInfoText, $step) //step data $dataStep = [ 'number' => $index, - 'type' => $stepType + 'type' => $stepType, + 'skippable' => $isSkippable, ]; //start step wrapper div @@ -791,7 +792,7 @@ public function createBody($index, $formInfoText, $step) $html .= Html::button( $this->iconAdd . ' Add', [ - 'class' => $this->classAdd . (($this->_bsVersion == 3) ? ' pull-right add_row' : ' float-right add_row') + 'class' => $this->classAdd . (($this->_bsVersion == 3) ? ' pull-right add_row' : ' float-right add_row'), // 'id'=>'add_row' ] ); @@ -887,7 +888,7 @@ function ($element) use ($model, $isTabularStep, $modelIndex) { } } - //generate the html for the step + //generate the html for the step $htmlFields .= $this->_createStepHtml($attributes, $modelIndex, $index, $model, $isTabularStep, $fieldConfig, $stepHeadings); //is tabular step @@ -929,7 +930,7 @@ public function createCustomInput($model, $attribute, $fieldConfig) [ 'template' => $template, 'options' => $containerOptions, - 'inputOptions' => $inputOptions + 'inputOptions' => $inputOptions, ], $isMultiField ); @@ -954,10 +955,10 @@ public function createCustomInput($model, $attribute, $fieldConfig) 'options' => $options, 'labelOptions' => $labelOptions, 'label' => $label, - 'itemsList' => $itemsList + 'itemsList' => $itemsList, ]; - //creae the field + //create the field return $this->_createField($fieldType, $fieldTypeOptions, $hintText); } @@ -976,14 +977,14 @@ public function registerScripts() //register plugin assets $this->_bsVersion == 3 ? - Bs3Assets::register($view) + Bs3Assets::register($view) : Bs4Assets::register($view); //is supported theme if (in_array($themeSelected, array_keys($this->themesSupported))) { $themeAsset = __NAMESPACE__ . '\assetbundles\bs' . - $this->_bsVersion . '\Theme' . - $this->themesSupported[$themeSelected] . 'Asset'; + $this->_bsVersion . '\Theme' . + $this->themesSupported[$themeSelected] . 'Asset'; $themeAsset::register($view); } diff --git a/src/assets/js/formwizard.js b/src/assets/js/formwizard.js index 963d154..730ec1a 100644 --- a/src/assets/js/formwizard.js +++ b/src/assets/js/formwizard.js @@ -247,20 +247,35 @@ $.formwizard = { }, bindAfterValidate: function (form) { $(form) + .on("beforeValidate", function (event, messages, deferreds) { + let formName = $(this).attr("id"); + let currentIndex = $.formwizard.helper.currentIndex(form); + const isSkippableStep = $("#step-" + currentIndex).data('step').skippable; + if (isSkippableStep) { + $.each($.formwizard.fields[formName][currentIndex], function (index, fieldId) { + $("#" + formName).yiiActiveForm("remove", fieldId); + }); + } + }) .on("afterValidate", function (event, messages, errorAttributes) { - //reset the current target button if not clicked on the next button + //reset the current target button if not clicked on the next button if ($.formwizard.resetCurrentTarget) { $.formwizard.currentButtonTarget = null; } + event.preventDefault(); + let formName = $(this).attr("id"); let currentIndex = $.formwizard.helper.currentIndex(form); const isLastStep = currentIndex == $(form + " .step-anchor").find("li").length - 1; + const isPreviewEnabled = $.formwizard.options[formName].enablePreview && isLastStep; + const isSkippableStep = $("#step-" + currentIndex).data('step').skippable; + let res; //check if the preview step then skip validation messages check - if ($.formwizard.options[formName].enablePreview && isLastStep) { + if (isPreviewEnabled || isSkippableStep) { res = 0; } else { res = $.formwizard.fields[formName][currentIndex].diff(messages); @@ -289,9 +304,11 @@ $.formwizard = { return false; }) .on("beforeSubmit", function (event) { + console.log('submit'); event.preventDefault(); if ($.formwizard.submit) { $.formwizard.persistence.clearStorage(); + console.log("returning"); return true; } return false; @@ -875,4 +892,4 @@ Array.prototype.diff = function (arr2) { } } return ret; -}; +}; \ No newline at end of file