I am working on a wizard that dynamically gets employees from my backend. The employee table is generated (with a radio-input-field) and then set to my HTML code:
$.ajax({
method: "get",
url: '/getEmployees/',
dataType: 'json',
data: {
ids: JSON.stringify(services)
},
async: false,
success: function(data) {
$.each(data.workers, function(i, v) {
html += "<tr>";
html += "<td class=\"text-center\">";
html += "<label><input type=\"radio\" value=\"" + v.Worker.id + "\" name=\"employeeInput\" id=\"employeeInput\" /></label>";
html += "</td>";
html += "</tr>";
});
$('#employee_items').empty().html(html);
// Add new field
$('#employeeInput').formValidation('addField');
}
});
I am validating my form-inputs with the jQuery plugin "formvalidation.io":
$('#employeeForm').formValidation({
framework: 'bootstrap',
fields: {
employeeInput: {
validators: {
notEmpty: {
message: 'Please choose an employee'
}
}
}
}
});
After trying for hours I found out that dynamically generated fields have to be added to the form validation manually:
http://formvalidation.io/examples/adding-dynamic-field/
I tried it but had no luck so far. How can I use the form-validation for the field employeeInput
when I add this field dynamically?
Try this:
$('#employeeForm').formValidation('addField', $("#employeeInput"));