| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243 |
- /**
- * Form Wizard
- */
-
- 'use strict';
-
- (function () {
- // flatpickrRange
- const flatpickrRange = document.querySelector('#dealDuration');
- if (flatpickrRange) {
- flatpickrRange.flatpickr({
- mode: 'range'
- });
- }
-
- // Init custom option check
- window.Helpers.initCustomOptionCheck();
- // Vertical Wizard
- // --------------------------------------------------------------------
-
- const wizardCreateDeal = document.querySelector('#wizard-create-deal');
- if (typeof wizardCreateDeal !== undefined && wizardCreateDeal !== null) {
- // Wizard form
- const wizardCreateDealForm = wizardCreateDeal.querySelector('#wizard-create-deal-form');
- // Wizard steps
- const wizardCreateDealFormStep1 = wizardCreateDealForm.querySelector('#deal-type');
- const wizardCreateDealFormStep2 = wizardCreateDealForm.querySelector('#deal-details');
- const wizardCreateDealFormStep3 = wizardCreateDealForm.querySelector('#deal-usage');
- const wizardCreateDealFormStep4 = wizardCreateDealForm.querySelector('#review-complete');
- // Wizard next prev button
- const wizardCreateDealNext = [].slice.call(wizardCreateDealForm.querySelectorAll('.btn-next'));
- const wizardCreateDealPrev = [].slice.call(wizardCreateDealForm.querySelectorAll('.btn-prev'));
-
- let validationStepper = new Stepper(wizardCreateDeal, {
- linear: true
- });
-
- // Deal Type
- const FormValidation1 = FormValidation.formValidation(wizardCreateDealFormStep1, {
- fields: {
- dealAmount: {
- validators: {
- notEmpty: {
- message: 'Please enter amount'
- },
- numeric: {
- message: 'The amount must be a number'
- }
- }
- },
- dealRegion: {
- validators: {
- notEmpty: {
- message: 'Please select region'
- }
- }
- }
- },
-
- plugins: {
- trigger: new FormValidation.plugins.Trigger(),
- bootstrap5: new FormValidation.plugins.Bootstrap5({
- // Use this for enabling/changing valid/invalid class
- // eleInvalidClass: '',
- eleValidClass: '',
- rowSelector: '.form-control-validation'
- }),
- autoFocus: new FormValidation.plugins.AutoFocus(),
- submitButton: new FormValidation.plugins.SubmitButton()
- }
- }).on('core.form.valid', function () {
- // Jump to the next step when all fields in the current step are valid
- validationStepper.next();
- });
-
- // select2 (Region)
- const dealRegion = $('#dealRegion');
- if (dealRegion.length) {
- dealRegion.wrap('<div class="position-relative"></div>');
- dealRegion
- .select2({
- placeholder: 'Select an region',
- dropdownParent: dealRegion.parent()
- })
- .on('change', function () {
- // Revalidate the region field when an option is chosen
- FormValidation1.revalidateField('dealRegion');
- });
- }
-
- // Deal Details
- const FormValidation2 = FormValidation.formValidation(wizardCreateDealFormStep2, {
- fields: {
- // * Validate the fields here based on your requirements
- dealTitle: {
- validators: {
- notEmpty: {
- message: 'Please enter deal title'
- }
- }
- },
- dealCode: {
- validators: {
- notEmpty: {
- message: 'Please enter deal code'
- },
- stringLength: {
- min: 4,
- max: 10,
- message: 'The deal code must be more than 4 and less than 10 characters long'
- },
- regexp: {
- regexp: /^[A-Z0-9]+$/,
- message: 'The deal code can only consist of capital alphabetical and number'
- }
- }
- }
- },
- plugins: {
- trigger: new FormValidation.plugins.Trigger(),
- bootstrap5: new FormValidation.plugins.Bootstrap5({
- // Use this for enabling/changing valid/invalid class
- // eleInvalidClass: '',
- eleValidClass: '',
- rowSelector: '.form-control-validation'
- }),
- autoFocus: new FormValidation.plugins.AutoFocus(),
- submitButton: new FormValidation.plugins.SubmitButton()
- }
- }).on('core.form.valid', function () {
- // Jump to the next step when all fields in the current step are valid
- validationStepper.next();
- });
-
- // select2 (Offered Item)
- const dealOfferedItem = $('#dealOfferedItem');
- if (dealOfferedItem.length) {
- dealOfferedItem.wrap('<div class="position-relative"></div>');
- dealOfferedItem
- .select2({
- placeholder: 'Select an offered item',
- dropdownParent: dealOfferedItem.parent()
- })
- .on('change', function () {
- // Revalidate the field if needed when an option is chosen
- // FormValidation2.revalidateField('dealOfferedItem');
- });
- }
-
- // Deal Usage
- const FormValidation3 = FormValidation.formValidation(wizardCreateDealFormStep3, {
- fields: {
- // * Validate the fields here based on your requirements
- },
- plugins: {
- trigger: new FormValidation.plugins.Trigger(),
- bootstrap5: new FormValidation.plugins.Bootstrap5({
- // Use this for enabling/changing valid/invalid class
- // eleInvalidClass: '',
- eleValidClass: '',
- rowSelector: '.form-control-validation'
- }),
- autoFocus: new FormValidation.plugins.AutoFocus(),
- submitButton: new FormValidation.plugins.SubmitButton()
- }
- }).on('core.form.valid', function () {
- validationStepper.next();
- });
-
- // Deal Usage
- const FormValidation4 = FormValidation.formValidation(wizardCreateDealFormStep4, {
- fields: {
- // * Validate the fields here based on your requirements
- },
- plugins: {
- trigger: new FormValidation.plugins.Trigger(),
- bootstrap5: new FormValidation.plugins.Bootstrap5({
- // Use this for enabling/changing valid/invalid class
- // eleInvalidClass: '',
- eleValidClass: '',
- rowSelector: '.form-control-validation'
- }),
- autoFocus: new FormValidation.plugins.AutoFocus(),
- submitButton: new FormValidation.plugins.SubmitButton()
- }
- }).on('core.form.valid', function () {
- // You can submit the form
- // wizardCreateDealForm.submit()
- // or send the form data to server via an Ajax request
- // To make the demo simple, I just placed an alert
- alert('Submitted..!!');
- });
-
- wizardCreateDealNext.forEach(item => {
- item.addEventListener('click', event => {
- // When click the Next button, we will validate the current step
- switch (validationStepper._currentIndex) {
- case 0:
- FormValidation1.validate();
- break;
-
- case 1:
- FormValidation2.validate();
- break;
-
- case 2:
- FormValidation3.validate();
- break;
-
- case 3:
- FormValidation4.validate();
- break;
-
- default:
- break;
- }
- });
- });
-
- wizardCreateDealPrev.forEach(item => {
- item.addEventListener('click', event => {
- switch (validationStepper._currentIndex) {
- case 3:
- validationStepper.previous();
- break;
-
- case 2:
- validationStepper.previous();
- break;
-
- case 1:
- validationStepper.previous();
- break;
-
- case 0:
-
- default:
- break;
- }
- });
- });
- }
- })();
|