Thanks to Andrey for pointing me in the right direction. I was able to clone the form and even add a link to delete the last cloned entries. This solution will not require me to create a form in javascript, so the whole format is still untouched and there is less code to write.
I know that this will ruin the Auth components and my form will be black as the hash will be wrong. I will have to manually clear it on the server later. Basically, I replace the first field with identifier 1 with the current increment. The three places that I see need a unique name and id - this is the label, login name and input identifier. I don't know if anyone has a better solution to replace this html value, but I have to convert it back and forth to replace it.
Here is my solution:
// HTML <div id="user"> <div class="users"> <h4>User #1</h4> <?php echo $form->input("User.1.first_name", array( 'label' => 'First Name', 'error' => 'Please enter a valid first name.' ));?> <?php echo $form->input("User.1.last_name", array( 'label' => 'First Name', 'error' => 'Please enter a valid last name.' ));?> </div> </div> <div id="moreUsers"></div>
// Javascript code:
var currentUserCount = 1; $('#addUser').click(function(){ currentUserCount = cloning('#user', '#moreUser', currentUserCount); return false; }); $('#removeUser').click(function(){ $('.users:last').remove(); currentUserCount--; } return false; }); function cloning(from, to, counter) { var clone = $(from).clone(); counter++; // Replace the input attributes: clone.find(':input').each(function() { var name = $(this).attr('name').replace(1,counter); var id = $(this).attr('id').replace(1,counter); $(this).attr({'name': name, 'id': id}).val(''); }); // Replace the label for attribute: clone.find('label').each(function() { var newFor = $(this).attr('for').replace(1,counter); $(this).attr('for', newFor); }); // Replace the text between html tags: clone = clone.html().replace(1,counter); $(to).before(clone); return counter; } // end cloning
source share