In this example, we will use
bootbox.js to display and validate a login form inside a modal. Initially, the login form isn't displayed:
<button type="submit" class="btn btn-default" id="loginButton">Login</button>
<form id="loginForm" method="POST" class="form-horizontal" style="display: none;">...</form>
const loginForm = document.getElementById('loginForm');
const fv = FormValidation.formValidation(loginForm, {
fields: {
...
},
plugins: {
...
},
});
document.getElementById('loginButton').addEventListener('click', function() {
bootbox
.dialog({
title: 'Login',
message: loginForm,
size: 'small',
show: false
})
.on('shown.bs.modal', function() {
loginForm.style.display = 'block';
fv.resetForm(true);
})
.on('hide.bs.modal', function() {
loginForm.style.display = 'none';
document.body.appendChild(loginForm);
})
.modal('show');
});
const fv = FormValidation
.formValidation(loginForm, {
fields: {
...
},
plugins: {
...
},
})
.on('core.form.valid', function() {
$.post($(loginForm).attr('action'), $(loginForm).serialize(), function(result) {
$(loginForm).parents('.bootbox').modal('hide');
}, 'json');
});