Formalidation bootstrap 4

136 views Asked by At

I'm trying to use the new version of Formvalidation.io (1.3.0) with Bootstrap 4.

I tried to write code as explained into their site but it doesn't work.

Here my code if someone had the same problem that solved...

document.addEventListener('DOMContentLoaded', function (e) {
        FormValidation.formValidation(
            document.getElementById('registerForm'),
            {
                fields: {
                    email: {
                        validators: {
                            notEmpty: {
                                message: 'Il campo è obbligatorio'
                            },
                            emailAddress: {
                                message: 'indirizzo inserito non è un indirizzo email valido'
                            }
                        }
                    },
                    cell: {
                        validators: {
                            notEmpty: {
                                message: 'Il campo è obbligatorio'
                            },
                        }
                    },
                },
                plugins: {
                    trigger: new FormValidation.plugins.Trigger(),
                    bootstrap: new FormValidation.plugins.Bootstrap(),
                    submitButton: new FormValidation.plugins.SubmitButton(),
                    icon: new FormValidation.plugins.Icon({
                        valid: 'fa fa-check',
                        invalid: 'fa fa-times',
                        validating: 'fa fa-refresh'
                    }),
                },
            }
        );
    });

https://jsfiddle.net/h678dz34/

Thanks!

2

There are 2 answers

0
Swim89 On BEST ANSWER

I solved moving submit button inside <form>. Here my corrected code:

<div class="modal fade" id="registerModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
  <form id="registerForm" method="POST">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title" id="exampleModalLabel">Registrazione</h5>
                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                    <span aria-hidden="true">&times;</span>
                </button>
            </div>
            <div class="modal-body">
                    <div class="form-group">
                        <label for="email">Email</label>
                        <input type="email" class="form-control" id="email" name="email" aria-describedby="emailHelp" placeholder="Indirizzo email">
                        <small id="emailHelp" class="form-text text-muted">Verrà utilizzato solo per inviare la conferma prenotazione.</small>
                    </div>
                    <div class="form-group">
                        <label for="cell">Cellulare</label>
                        <input type="tel" class="form-control" id="cell" name="cell" aria-describedby="cellHelp" placeholder="Num. Cellulare">
                        <small id="emailHelp" class="form-text text-muted">Verrà utilizzato solo per comunicazioni inerenti l'evento.</small>
                    </div>
                    <div class="form-group" id="group_maxPax">
                        <label for="numPax">Num. Partecipanti</label>
                        <input type="text" class="form-control" id="numPax" name="numPax" aria-describedby="paxHelp" placeholder="Partecipanti">
                        <small id="paxHelp" class="form-text text-muted"></small>
                    </div>

            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-secondary" data-dismiss="modal">Chiudi</button>
                <button type="submit" class="btn btn-success" name="submitButton" id="saveRegister">Completa registrazione
                    <i class="fas fa-user-check"></i></button>
            </div>
        </div>
    </div>
  </form>
</div>
0
Samuel C. On

You cannot instantiate the formValidation object on not displayed DOM.

You need to instantiate it once the modal is visible by using the modal event `shown.bs.modal` for instance.

$('#myModal').on('shown.bs.modal', function(e) {
  // do something...
});