Validation d’amorçage lors de l’ajout dynamic de plusieurs champs

J’utilise bootstrap v3.1.1 et je veux valider un formulaire avec validation de bootstrap mais qui contient un bouton pour cloner 3 champs. Avec le clonage, tout fonctionne bien, mais je ne peux pas valider les champs clonés. Voici mon formulaire HTML:

add line

Dans le fichier JavaScript j’ai:

  $(document).ready(function () { var count = 2; $('#cloneButton').click(function () { var klon = $('#line_1'); klon.clone().attr('id', 'line_' + (++count)).insertAfter($('#line_1')); $('#line_' + count).children('div').children('input').each(function () { $(this).val(''); var oldId = $(this).attr('id').split('_'); $(this).attr('id', oldId[0] + '_' + count); }); }); //For validating the fields: $('#myForm').bootstrapValidator({ fields: { 'firstField[]': { validators: { notEmpty: { message: 'Enter a value' } } }, 'secondField[]': { validators: { notEmpty: { message: 'Enter a value' } } }, 'thirdField[]': { validators: { notEmpty: { message: 'Enter a value' } } } } }); }); 

Je maintenant que je dois utiliser quelque chose comme ça

 $('#myForm').bootstrapValidator('addField', klon.find('[name="firstField[]"]')); 

pour chaque champ, mais je ne sais pas maintenant comment le faire correctement. Aidez-moi, s’il vous plaît. Merci!!

si vous insérez la méthode addField dans votre boucle de input , cela devrait fonctionner. Je suggère également d’enregistrer votre première row tant que modèle.

 var template = $('#line_1').clone(); var options = { fields: { 'firstField[]': { validators: { notEmpty: { message: 'Enter a value 1' } } }, 'secondField[]': { validators: { notEmpty: { message: 'Enter a value 2' } } }, 'thirdField[]': { validators: { notEmpty: { message: 'Enter a value 3' } } } } }; $('#myForm').bootstrapValidator(options); $('#cloneButton').click(function () { var rowId = $('.row').length + 1; var validator = $('#myForm').data('bootstrapValidator'); var klon = template.clone(); klon.attr('id', 'line_' + rowId) .insertAfter($('.row').last()) .find('input') .each(function () { $(this).attr('id', $(this).attr('id').replace(/_(\d*)$/, "_"+rowId)); validator.addField($(this)); }) }); 

VIOLON

Cette réponse ne correspond pas exactement à cette question, mais si quelqu’un souhaite append un champ différent de manière dynamic, il peut l’utiliser.

 $('#myForm').formValidation('addField', 'fourthField[]', { validators: { notEmpty: { message: 'Enter a value 4' } }, }); 

Syntaxe: $("#form_name").formValidation("addField",'no of the field',{validation});