Advertisement
RupeshAcharya60

resident form

Jul 23rd, 2024
18
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.18 KB | None | 0 0
  1. {% extends 'dashboard/base.html' %}
  2. {% block content %}
  3. <form method="post">
  4. {% csrf_token %}
  5. {{ wizard.management_form }}
  6.  
  7. <h2>Primary Applicant Addresses</h2>
  8. {{ wizard.form.primary_addresses|pprint }}
  9.  
  10. {{ wizard.form.primary_addresses.management_form }}
  11. <div id="primary-addresses">
  12. {% for form in wizard.form.primary_addresses %}
  13. <div class="address-form">
  14.  
  15. {{ form.as_p }}
  16. </div>
  17. {% endfor %}
  18. </div>
  19. <button type="button" onclick="addForm('primary')">Add Primary Address</button>
  20.  
  21. <h2>Secondary Applicant Addresses</h2>
  22. {{ wizard.form.secondary_addresses.management_form }}
  23. <div id="secondary-addresses">
  24. {% for form in wizard.form.secondary_addresses %}
  25. <div class="address-form">
  26. {{ form.as_p }}
  27. </div>
  28. {% endfor %}
  29. </div>
  30. <button type="button" onclick="addForm('secondary')">Add Secondary Address</button>
  31.  
  32. <button type="submit">{% if wizard.steps.prev %}Next Step{% else %}Submit{% endif %}</button>
  33. </form>
  34.  
  35. <script>
  36.  
  37. function addForm(prefix) {
  38. const formsDiv = document.getElementById(`${prefix}-addresses`);
  39. const totalForms = document.getElementById(`id_${prefix}-TOTAL_FORMS`);
  40.  
  41. if (!totalForms) {
  42. console.error(`Total forms element not found for ${prefix}`);
  43. return;
  44. }
  45. console.log(totalForms.value)
  46.  
  47. const newForm = formsDiv.children[0].cloneNode(true);
  48. const formRegex = new RegExp(`${prefix}_addresses-(\\d+)-`, 'g');
  49. const formNum = totalForms.value;
  50.  
  51. console.log(formRegex)
  52.  
  53. newForm.innerHTML = newForm.innerHTML.replace(formRegex, `${prefix}_addresses-${formNum}-`);
  54.  
  55.  
  56. newForm.querySelectorAll('input').forEach(element => {
  57. element.value = '';
  58. if (element.id) {
  59. element.id = element.id.replace("100");
  60. }
  61. if (element.name) {
  62. element.name = element.name.replace("100");
  63. }
  64. });
  65. console.log(newForm)
  66.  
  67.  
  68.  
  69.  
  70.  
  71.  
  72. formsDiv.appendChild(newForm);
  73.  
  74.  
  75.  
  76.  
  77.  
  78.  
  79.  
  80. totalForms.value = parseInt(totalForms.value) + 1;
  81. }
  82. </script>
  83. {% endblock %}
  84.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement