jquerylaravelsmart-wizard

Use custom validation in smart wizard?


I want to ask if I can use Laravel validation in the Smart Wizard.

my example takes an error message from laravel to appear in the smart wizard.

In this stage I followed the example of the official web documentation.

this is the code that I made:

in file .blade.php

@extends('layouts.base')

@section('before_script')
    <link rel="stylesheet" href="{{ asset('assets/css/smart_wizard.min.css') }}">
    <link rel="stylesheet" href="{{ asset('assets/css/smart_wizard_theme_dots.min.css') }}">
    <script src="{{ asset('assets/js/jquery.smartWizard.min.js')}}"></script>
    <script src="{{ asset('assets/js/bootstrap-validation.min.js') }}"></script>
    <style>
        .sw-btn-group {
            width: 100%;
            height: auto;
        }
    </style>
    <script>
        $(document).ready(function () {
            $('#smartwizard').smartWizard({
                theme: 'dots',
                transitionEffect: 'fade',
                transitionSpeed: '400',
                lang: { next: 'Selanjutnya', previous: 'Kembali'},
                toolbarSettings: {
                    toolbarButtonPosition: 'left',
                    showNextButton: true,
                    showPreviousButton: true
                }
            })

            $('#smartwizard').on("leaveStep", function(e, anchorObject, stepNumber, stepDirection) {
                var elmForm = $("#form-step-" + stepNumber);
                if (stepDirection === 'forward' && elmForm) {
                    elmForm.validator('validate');
                    var elmErr = elmForm.children('.has-error');
                    if (elmErr && elmErr.length > 0) {
                        return false;
                    }
                }
                return true;
            })
        })

    </script>
@endsection

@section('content')
<section>
    <div class="container">
        {{-- <br><br> --}}
        <form action="#" id="myForm" role="form" data-toggle="validator" method="post" accept-charset="utf-8">
            <div id="smartwizard">
                <ul>
                    <li><a href="#step-1">Step Title<br /><small>Step description</small></a></li>
                    <li><a href="#step-2">Step Title<br /><small>Step description</small></a></li>
                    <li><a href="#step-3">Step Title<br /><small>Step description</small></a></li>
                    <li><a href="#step-4">Step Title<br /><small>Step description</small></a></li>
                </ul>

                <div>
                    <div id="step-1" class="">
                        <h2>Your Name</h2>
                        <div id="form-step-0" role="form" data-toggle="validator">
                            <div class="form-group">
                                <label for="name">Name: </label>
                                <input type="text" name="name" id="name" class="form-control" required placeholder="enter your name">
                                <div class="help-block with-errors"></div>
                            </div>

                            <div class="form-group">
                                <label class="control-label">Pin</label>
                                <input id="pin" type="tel" pattern="[0-9]*" inputmode="numeric" style="-webkit-text-security: disc;" autocomplete="off" maxlength="6" name="pin" placeholder="{{ __('PIN') }}"
                                    class="{{ $errors->has('pin') ? ' is-invalid form-control' : 'form-control' }}" onkeypress="checkPin(event);" required/>

                                @if ($errors->has('pin'))
                                    <div class="help-block with-errors">{{$errors->first('pin')}}</div>
                                @endif
                            </div>
                            <div class="form-group">
                                <label class="control-label">confirm</label>
                                <input type="tel" name="pin_confirmation" pattern="[0-9]*" inputmode="numeric" style="-webkit-text-security: disc;" autocomplete="off"
                                    placeholder="{{ __('confirm) }}" class="form-control" maxlength="6" onkeypress="checkPin(event);" required/>
                                @if ($errors->has('pin'))
                                    <span class="invalid-feedback">
                                        <strong>{{ $errors->first('pin_confirmation') }}</strong>
                                    </span>
                                @endif
                            </div>
                        </div>
                    </div>
                    <div id="step-2" class="">
                        Step Content
                    </div>
                    <div id="step-3" class="">
                        Step Content
                    </div>
                    <div id="step-4" class="">
                        Step Content
                    </div>
                </div>
            </div>
        </form>
    </div>
</section>
@endsection

Here I am trying to enter the input pin and issue an error message from laravel.

Error messages do not appear, but the form is still required until I have filled the form correctly.

Are there other ways? or indeed I can only use it as its documentation.

Thanks


Solution

  • Finally i found the solution i just need to change some of my code.

    hopefully this can help others when using smart wizard. :)

    <script>
        $(document).ready(function () {
            $('#smartwizard').smartWizard({
                theme: 'dots',
                transitionEffect: 'fade',
                transitionSpeed: '400',
                lang: { next: 'Selanjutnya', previous: 'Kembali'},
                toolbarSettings: {
                    toolbarButtonPosition: 'left',
                    showNextButton: true,
                    showPreviousButton: true
                }
            })
    
            $('#myForm').validate({
                rules: {
                    name: {
                        required: true
                    }
                },
                messages: {
                    name: {
                        required: 'Please Enter Your Name'
                    }
                }
            });
    
            $('#smartwizard').on("leaveStep", function(e, anchorObject, stepNumber, stepDirection) {
                var elmForm = $("#form-step-" + stepNumber);
                if (stepDirection === 'forward' && elmForm) {
                    // elmForm.validator('validate');
                    // var elmErr = elmForm.children('.has-error');
                    // if (elmErr && elmErr.length > 0) {
                    //     return false;
                    // }
                    if ($('#myForm').valid()) {
                        return true
                    } else {
                        return false
                    }
                }
                return true;
            })
        })
    </script>
    

    In this case I use jquery validation plugins