0

In my work I used to answer this question:

JQuery clone form and increment

How to validate Cloning a form using JQuery Validation?

I tried the following, but it didn't work:

    $(document).ready(function () {
    $(function () {
        var template = $('#attendees .attendee:first').clone(),
            attendeesCount = 1;
        var addAttendee = function () {
            attendeesCount++;
            var nativeTemplate = template.clone();
            var attendee = nativeTemplate.find(':input').each(function () {
                var newId = this.id.substring(0, this.id.length - 1) + attendeesCount;
                $(this).prev().attr('for', newId); // update label for (assume prev sib is label)
                this.name = this.id = newId; // update id and name (assume the same)
            }).end() // back to .attendee
                .attr('id', 'att' + attendeesCount) // update attendee id
                .prependTo('#attendees') // add to container
                .validate(
                    // rules
                );
        };
        $('.add').click(addAttendee); // attach event
    });
});
Community
  • 1
  • 1
roman-wm
  • 1
  • 2

3 Answers3

0

Before you call validate again, you need to get rid of the old validator data attached to the cloned form.

So the end of your code would look like this:

           .prependTo('#attendees') // add to container
           .removeData('validator') //get rid of cloned validator
            .validate(
                // rules
            );
Ryley
  • 21,046
  • 2
  • 67
  • 81
0

Try сlone(true), it will attach event handlers to cloned objects, it may help.

-1

I used this approach:

//Clone the form
var clonedForm = $('.original-form').clone(true);

//Get original form & validator
var originalform = $('.original-form').find('form');
var originalValidator = originalform.data('validator');

//Set validator to cloned form in popup
originalValidator.currentForm = clonedForm;

//Re-Set the validator to the cloned form
clonedForm.data('validator', originalValidator);

//Now you can validate the clonedForm by calling "valid()".
$(clonedForm).valid()

Hope this helps.

nhd86
  • 1