The plugin only provides error message option which is shown when the field is invalid. Is it possible to add a valid message option shown if the field is valid?
The quick answer is YES. This example shows you how to implement this feature.
Assuming that our form has two text boxes which are username and password.
<form id="signupForm" method="post" class="form-horizontal">
<div class="form-group">
<label class="col-lg-3 control-label">Username</label>
<div class="col-lg-5">
<input type="text" class="form-control" name="username" />
</div>
</div>
<div class="form-group">
<label class="col-lg-3 control-label">Password</label>
<div class="col-lg-5">
<input type="password" class="form-control" name="password" />
</div>
</div>
<div class="form-group">
<div class="col-lg-5 col-lg-offset-3">
<button type="submit" class="btn btn-default">Signup</button>
</div>
</div>
</form>
As usual, the plugin is called by following snippet code:
$(document).ready(function() {
$('#signupForm').bootstrapValidator({
feedbackIcons: {
...
},
fields: {
username: {
validators: {
...
}
},
password: {
validators: {
...
}
}
}
});
});
The implementation idea consists of four steps:
I add an option named validMessage
for each field. As the name suggests, the plugin will show the valid message when the field is valid.
$(document).ready(function() {
$('#signupForm').bootstrapValidator({
feedbackIcons: {
...
},
fields: {
username: {
validMessage: 'The username looks great',
validators: {
...
}
},
password: {
validMessage: 'The password is good',
validators: {
...
}
}
}
});
});
We can get the option value later via the getOptions()
method.
Basically, you can place the valid message anywhere you want as long as you can retrieve it later.
Behind the scenes, BootstrapValidator places each error message in a span
element. The field error messages are placed inside an element which can be retrieved by $field.data('bv.messages')
.
To make the example easily to follow, I simply create a span
element right after each field for showing the valid message:
<form id="signupForm" method="post" class="form-horizontal">
<div class="form-group">
<label class="col-lg-3 control-label">Username</label>
<div class="col-lg-5">
<input type="text" class="form-control" name="username" />
<span class="help-block validMessage"></span>
</div>
</div>
<div class="form-group">
<label class="col-lg-3 control-label">Password</label>
<div class="col-lg-5">
<input type="password" class="form-control" name="password" />
<span class="help-block validMessage"></span>
</div>
</div>
...
</form>
In fact, the number of fields might be greater than two as in our example. Therefore, it is much better if these valid message elements are created dynamically. It's quite easy to do it by triggering the init.field.bv
event:
$(document).ready(function() {
$('#signupForm')
.on('init.field.bv', function(e, data) {
var field = data.field, // Get the field name
$field = data.element, // Get the field element
bv = data.bv; // BootstrapValidator instance
// Create a span element to show valid message
// and place it right before the field
var $span = $('<small/>')
.addClass('help-block validMessage')
.attr('data-field', field)
.insertAfter($field)
.hide();
// Retrieve the valid message via getOptions()
var message = bv.getOptions(field).validMessage;
if (message) {
$span.html(message);
}
})
.bootstrapValidator({
...
});
});
The init.field.bv
event is triggered after the field is initialized by the plugin. In order to trigger the event, you must declare .on('init.field.bv')
before calling bootstrapValidator(options)
.
In step 2 above, the custom option validMessage
is determined by using the getOptions()
method.
Triggering the success.field.bv
event to do that:
$(document).ready(function() {
$('#signupForm')
.on('init.field.bv', function(e, data) {
...
})
.bootstrapValidator({
...
})
.on('success.field.bv', function(e, data) {
var field = data.field, // Get the field name
$field = data.element; // Get the field element
// Retrieve the valid message element
$field
.next('.validMessage[data-field="' + field + '"]')
.show(); // Show it
});
});
Finally, being similar to the step 3, the valid message must be hidden when the field turns to be invalid. It can be done via the error.field.bv
event:
$(document).ready(function() {
$('#signupForm')
.on('init.field.bv', function(e, data) {
...
})
.bootstrapValidator({
...
})
.on('success.field.bv', function(e, data) {
...
})
.on('error.field.bv', function(e, data) {
var field = data.field, // Get the field name
$field = data.element; // Get the field element
// Hide the valid message element
$field
.next('.validMessage[data-field="' + field + '"]')
.hide();
});
});
Below is the final working example:
<form id="signupForm" method="post" class="form-horizontal">
<div class="form-group">
<label class="col-lg-3 control-label">Username</label>
<div class="col-lg-5">
<input type="text" class="form-control" name="username" />
</div>
</div>
<div class="form-group">
<label class="col-lg-3 control-label">Password</label>
<div class="col-lg-5">
<input type="password" class="form-control" name="password" />
</div>
</div>
<div class="form-group">
<div class="col-lg-5 col-lg-offset-3">
<button type="submit" class="btn btn-default">Signup</button>
</div>
</div>
</form>
$(document).ready(function() {
$('#signupForm')
.on('init.field.bv', function(e, data) {
var field = data.field, // Get the field name
$field = data.element, // Get the field element
bv = data.bv; // BootstrapValidator instance
// Create a span element to show valid message
// and place it right before the field
var $span = $('<small/>')
.addClass('help-block validMessage')
.attr('data-field', field)
.insertAfter($field)
.hide();
// Retrieve the valid message via getOptions()
var message = bv.getOptions(field).validMessage;
if (message) {
$span.html(message);
}
})
.bootstrapValidator({
feedbackIcons: {
valid: 'glyphicon glyphicon-ok',
invalid: 'glyphicon glyphicon-remove',
validating: 'glyphicon glyphicon-refresh'
},
fields: {
username: {
validMessage: 'The username looks great',
validators: {
notEmpty: {
message: 'The username is required'
},
different: {
field: 'password',
message: 'The username cannot be same as password'
},
regexp: {
regexp: /^[a-zA-Z0-9]+$/i,
message: 'The username can only consist of alphabetical, number'
}
}
},
password: {
validMessage: 'The password is good',
validators: {
notEmpty: {
message: 'The password is required'
},
different: {
field: 'username',
message: 'The password cannot be same as username'
}
}
}
}
})
.on('success.field.bv', function(e, data) {
var field = data.field, // Get the field name
$field = data.element; // Get the field element
// Show the valid message element
$field.next('.validMessage[data-field="' + field + '"]').show();
})
.on('error.field.bv', function(e, data) {
var field = data.field, // Get the field name
$field = data.element; // Get the field element
// Show the valid message element
$field.next('.validMessage[data-field="' + field + '"]').hide();
});
});