I am trying to create a modal using Bootbox. I have the modal popup and ask you to fill in some data. I am then trying to do validation so when they click on save, it checks to just make sure the fields are filled in.
How can I prevent the modal from closing when clicking save if validation fails?
bootbox.dialog(header + content, [{
"label": "Save",
"class": "btn-primary",
"callback": function() {
title = $("#title").val();
description = $("#description").val();
icon = $("#icon").val();
href = $("#link").val();
newWindow = $("#newWindow").val();
type = $("#type").val();
group = $("#group").val();
if (!title){ $("#titleDiv").attr('class', 'control-group error'); } else {
addMenu(title, description, icon, href, newWindow, type, group);
}
}
}, {
"label": "Cancel",
"class": "btn",
"callback": function() {}
}]);
I am trying to create a modal using Bootbox. I have the modal popup and ask you to fill in some data. I am then trying to do validation so when they click on save, it checks to just make sure the fields are filled in.
How can I prevent the modal from closing when clicking save if validation fails?
bootbox.dialog(header + content, [{
"label": "Save",
"class": "btn-primary",
"callback": function() {
title = $("#title").val();
description = $("#description").val();
icon = $("#icon").val();
href = $("#link").val();
newWindow = $("#newWindow").val();
type = $("#type").val();
group = $("#group").val();
if (!title){ $("#titleDiv").attr('class', 'control-group error'); } else {
addMenu(title, description, icon, href, newWindow, type, group);
}
}
}, {
"label": "Cancel",
"class": "btn",
"callback": function() {}
}]);
Share
Improve this question
asked Aug 25, 2013 at 0:41
SBBSBB
8,97035 gold badges118 silver badges233 bronze badges
2 Answers
Reset to default 13I think you can just return false in your "Save" button callback
like this:
bootbox.dialog(header + content, [{
"label": "Save",
"class": "btn-primary",
"callback": function() {
title = $("#title").val();
description = $("#description").val();
icon = $("#icon").val();
href = $("#link").val();
newWindow = $("#newWindow").val();
type = $("#type").val();
group = $("#group").val();
if (!title){
$("#titleDiv").attr('class', 'control-group error');
return false;
} else {
addMenu(title, description, icon, href, newWindow, type, group);
}
}
}, {
"label": "Cancel",
"class": "btn",
"callback": function() {}
}]);
As mented by @AjeetMalviya, the solution posted by @bruchowski does not close the Bootbox when using return false;
that way. The callback returns null
when the Cancel button is clicked and an empty string when the OK button is clicked.
<script>
var bb = bootbox.prompt({
title: 'Input Required',
onEscape: true,
buttons: {
confirm: {
label: '<svg><use xlink:href="/icons.svg#check" /></svg> OK'
},
cancel: {
label: '<svg><use xlink:href="/icons.svg#x" /></svg> Cancel',
className: 'btn-secondary'
}
},
inputType: 'password',
callback: function (result) {
//result is null when Cancel is clicked
//empty when OK is clicked
if (result === null) {
return;
} else if (result === '') {
bb.find('.bootbox-input-password').addClass('input-validation-error');
return false;
}
console.log(result);
}
});
bb.init(function () {
//do stuff with the bootbox on startup here
});
</script>