I have a "create new" button linked to a bootbox modal form that asks the user to input the name of a new chat room. The code looks like this:
Template.chatsList.events = {
'click .newchat-button': function () {
bootbox.prompt("Enter a title for your new chat", function(result) {
if (result != null) {
var validEntry = result;
Rooms.insert({
name: validEntry
});
}
});
}
}
However, on a blank submit, the submission still goes through and the room name is blank. How can I stop this from happening? I tried result.length > 1, etc, to no avail.
I'm a beginner and would really appreciate some explanation on the matter.
bootbox.prompt()
will execute the callback regardless of what the input is, even if blank. A common pattern instead is to use a custombootbox.dialog()
and disable the submit button until something has been input. You'd still allow cancel of course.