There are many developers asking me for an example that demonstrates the ability of showing messages in custom area.
Here are few of scenarios which you might want to use this feature:
There are two ways to solve this requirement which are listed in the next sections.
The first solution which is easiest way is to use the container option.
This option indicates where the error messages are shown.
Value | Description |
---|---|
CSS selector | All error messages are placed in element defined by this CSS selector |
tooltip |
Error messages of each field are placed inside a Bootstrap tooltip. |
popover |
Error messages of each field are placed inside a Bootstrap popover. |
The contact form below shows messages in an element located at the bottom of form.
<form id="contactForm" method="post" class="form-horizontal">
<div class="form-group">
<label class="col-md-3 control-label">Full name</label>
<div class="col-md-6">
<input type="text" class="form-control" name="fullName" />
</div>
</div>
<div class="form-group">
<label class="col-md-3 control-label">Email</label>
<div class="col-md-6">
<input type="text" class="form-control" name="email" />
</div>
</div>
<div class="form-group">
<label class="col-md-3 control-label">Title</label>
<div class="col-md-6">
<input type="text" class="form-control" name="title" />
</div>
</div>
<div class="form-group">
<label class="col-md-3 control-label">Content</label>
<div class="col-md-6">
<textarea class="form-control" name="content" rows="5"></textarea>
</div>
</div>
<!-- #messages is where the messages are placed inside -->
<div class="form-group">
<div class="col-md-9 col-md-offset-3">
<div id="messages"></div>
</div>
</div>
<div class="form-group">
<div class="col-md-9 col-md-offset-3">
<button type="submit" class="btn btn-default">Validate</button>
</div>
</div>
</form>
$(document).ready(function() {
$('#contactForm').bootstrapValidator({
container: '#messages',
feedbackIcons: {
valid: 'glyphicon glyphicon-ok',
invalid: 'glyphicon glyphicon-remove',
validating: 'glyphicon glyphicon-refresh'
},
fields: {
fullName: {
validators: {
notEmpty: {
message: 'The full name is required and cannot be empty'
}
}
},
email: {
validators: {
notEmpty: {
message: 'The email address is required and cannot be empty'
},
emailAddress: {
message: 'The email address is not valid'
}
}
},
title: {
validators: {
notEmpty: {
message: 'The title is required and cannot be empty'
},
stringLength: {
max: 100,
message: 'The title must be less than 100 characters long'
}
}
},
content: {
validators: {
notEmpty: {
message: 'The content is required and cannot be empty'
},
stringLength: {
max: 500,
message: 'The content must be less than 500 characters long'
}
}
}
}
});
});
Using the container
option works fine. The messages are placed inside span
elements with .help-block
class. You might will ask for more customizations such as:
li
element, therefore I can customize the style laterIt's possible to solve these requirements by using the methods and events below:
Event/Method | Description |
---|---|
success.form.bv event | Triggered when the form is valid |
error.field.bv event | Triggered when any field is invalid |
success.field.bv event | Triggered when any field is valid |
getMessages(field) method | Get the field messages |
Looking at the HTML and JS tabs to see how we get the job done.
<style type="text/css">
/* Simple message styles customization */
#errors {
border-left: 5px solid #a94442;
padding-left: 15px;
}
#errors li {
list-style-type: none;
}
#errors li:before {
content: '\b7\a0';
}
</style>
<form id="sendMessageForm" method="post" class="form-horizontal">
<div class="form-group">
<label class="col-md-3 control-label">Full name</label>
<div class="col-md-6">
<input type="text" class="form-control" name="fullName" />
</div>
</div>
<div class="form-group">
<label class="col-md-3 control-label">Email</label>
<div class="col-md-6">
<input type="text" class="form-control" name="email" />
</div>
</div>
<div class="form-group">
<label class="col-md-3 control-label">Title</label>
<div class="col-md-6">
<input type="text" class="form-control" name="title" />
</div>
</div>
<div class="form-group">
<label class="col-md-3 control-label">Content</label>
<div class="col-md-6">
<textarea class="form-control" name="content" rows="5"></textarea>
</div>
</div>
<!-- Show the messages in #errors -->
<div class="form-group">
<div class="col-md-9 col-md-offset-3">
<ul id="errors"></ul>
</div>
</div>
<div class="form-group">
<div class="col-md-9 col-md-offset-3">
<button type="submit" class="btn btn-default">Validate</button>
</div>
</div>
</form>
$(document).ready(function() {
$('#sendMessageForm')
.bootstrapValidator({
feedbackIcons: {
valid: 'glyphicon glyphicon-ok',
invalid: 'glyphicon glyphicon-remove',
validating: 'glyphicon glyphicon-refresh'
},
fields: {
fullName: {
validators: {
notEmpty: {
message: 'The full name is required and cannot be empty'
}
}
},
email: {
validators: {
notEmpty: {
message: 'The email address is required and cannot be empty'
},
emailAddress: {
message: 'The email address is not valid'
}
}
},
title: {
validators: {
notEmpty: {
message: 'The title is required and cannot be empty'
},
stringLength: {
max: 100,
message: 'The title must be less than 100 characters long'
}
}
},
content: {
validators: {
notEmpty: {
message: 'The content is required and cannot be empty'
},
stringLength: {
max: 500,
message: 'The content must be less than 500 characters long'
}
}
}
}
})
.on('success.form.bv', function(e) {
// Reset the message element when the form is valid
$('#errors').html('');
})
.on('error.field.bv', function(e, data) {
// data.bv --> The BootstrapValidator instance
// data.field --> The field name
// data.element --> The field element
// Get the messages of field
var messages = data.bv.getMessages(data.element);
// Remove the field messages if they're already available
$('#errors').find('li[data-field="' + data.field + '"]').remove();
// Loop over the messages
for (var i in messages) {
// Create new 'li' element to show the message
$('<li/>')
.attr('data-field', data.field)
.wrapInner(
$('<a/>')
.attr('href', 'javascript: void(0);')
.html(messages[i])
.on('click', function(e) {
// Focus on the invalid field
data.element.focus();
})
)
.appendTo('#errors');
}
// Hide the default message
// $field.data('bv.messages') returns the default element containing the messages
data.element
.data('bv.messages')
.find('.help-block[data-bv-for="' + data.field + '"]')
.hide();
})
.on('success.field.bv', function(e, data) {
// Remove the field messages
$('#errors').find('li[data-field="' + data.field + '"]').remove();
});
});