When working with complex form, the fields might be added to (or remove from) the form dynamically. The newly added fields also need to be validated.
This example demonstrates a sample scenario where you have to solve validating dynamic fields problem.
Before going to the details, there are some methods and events you need to know:
Method/Event | Description |
---|---|
addField() method | Adding new field |
removeField() method | Removing given field |
added.field.bv event | Called after adding new field |
removed.field.bv event | Called after removing given field |
Assuming that you are managing a survey which consists of a question and some options that user can choose from. The form allows you to add more options but the number of options can't exceed 5.
<form id="surveyForm" method="post" class="form-horizontal">
<div class="form-group">
<label class="col-md-3 control-label">Question</label>
<div class="col-md-5">
<input type="text" class="form-control" name="question" />
</div>
</div>
<div class="form-group">
<label class="col-md-3 control-label">Options</label>
<div class="col-md-5">
<input type="text" class="form-control" name="option[]" />
</div>
<div class="col-md-4">
<button type="button" class="btn btn-default addButton">
<i class="fa fa-plus"></i>
</button>
</div>
</div>
<!-- The option field template containing an option field and a Remove button -->
<div class="form-group hide" id="optionTemplate">
<div class="col-md-offset-3 col-md-5">
<input type="text" class="form-control" name="option[]" />
</div>
<div class="col-md-4">
<button type="button" class="btn btn-default removeButton">
<i class="fa fa-minus"></i>
</button>
</div>
</div>
<div class="form-group">
<div class="col-md-5 col-md-offset-3">
<button type="submit" class="btn btn-default">Validate</button>
</div>
</div>
</form>
$(document).ready(function() {
// The maximum number of options
var MAX_OPTIONS = 5;
$('#surveyForm')
.bootstrapValidator({
feedbackIcons: {
valid: 'glyphicon glyphicon-ok',
invalid: 'glyphicon glyphicon-remove',
validating: 'glyphicon glyphicon-refresh'
},
fields: {
question: {
validators: {
notEmpty: {
message: 'The question required and cannot be empty'
}
}
},
'option[]': {
validators: {
notEmpty: {
message: 'The option required and cannot be empty'
},
stringLength: {
max: 100,
message: 'The option must be less than 100 characters long'
}
}
}
}
})
// Add button click handler
.on('click', '.addButton', function() {
var $template = $('#optionTemplate'),
$clone = $template
.clone()
.removeClass('hide')
.removeAttr('id')
.insertBefore($template),
$option = $clone.find('[name="option[]"]');
// Add new field
$('#surveyForm').bootstrapValidator('addField', $option);
})
// Remove button click handler
.on('click', '.removeButton', function() {
var $row = $(this).parents('.form-group'),
$option = $row.find('[name="option[]"]');
// Remove element containing the option
$row.remove();
// Remove field
$('#surveyForm').bootstrapValidator('removeField', $option);
})
// Called after adding new field
.on('added.field.bv', function(e, data) {
// data.field --> The field name
// data.element --> The new field element
// data.options --> The new field options
if (data.field === 'option[]') {
if ($('#surveyForm').find(':visible[name="option[]"]').length >= MAX_OPTIONS) {
$('#surveyForm').find('.addButton').attr('disabled', 'disabled');
}
}
})
// Called after removing the field
.on('removed.field.bv', function(e, data) {
if (data.field === 'option[]') {
if ($('#surveyForm').find(':visible[name="option[]"]').length < MAX_OPTIONS) {
$('#surveyForm').find('.addButton').removeAttr('disabled');
}
}
});
});