org-form.js 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. (function ($){
  2. let orgForm = $('#org-form');
  3. let curStep = 0;
  4. let tabCount = document.getElementsByClassName("tab").length;
  5. $('#organizations-short_name').focus();
  6. $('#orgFormContainer .tab').each(function (index){
  7. curStep = $(this).is('.d-block') ? index : 0;
  8. });
  9. $('#prevBtn').click(function (){
  10. $(".tab:eq("+curStep+")").hide().prev().show();
  11. curStep -= 1;
  12. if(curStep === 0){
  13. $(this).addClass('d-none');
  14. $('#nextBtn').removeClass('d-none');
  15. }
  16. console.log(curStep);
  17. });
  18. $('#nextBtn').click(function (){
  19. console.log(curStep);
  20. if(curStep === 0) {
  21. let valid;
  22. orgForm.yiiActiveForm('validate', true);
  23. orgForm.on('afterValidate', function (event, attribute, messages) {
  24. console.log(messages);
  25. if(messages.length > 0) {
  26. valid = false;
  27. }
  28. if(!valid) return false;
  29. });
  30. if(!valid) return false;
  31. }
  32. $(".tab:eq("+curStep+")").hide().next().show();
  33. curStep += 1;
  34. if(curStep >= (tabCount-1)) {
  35. $(this).addClass('d-none');
  36. }
  37. $('#prevBtn').removeClass('d-none');
  38. });
  39. orgForm.on('beforeSubmit', function () {
  40. var $yiiform = $(this);
  41. $.ajax({
  42. type: $yiiform.attr('method'),
  43. url: $yiiform.attr('action'),
  44. data: $yiiform.serializeArray(),
  45. }
  46. )
  47. .done(function(data) {
  48. if(data.success) {
  49. // data is saved
  50. $yiiform.find('input[name=id]').val(data.id)
  51. $yiiform.attr('action', '/organizations/update?id='+data.id)
  52. //goNext();
  53. } else if (data.validation) {
  54. // server validation failed
  55. console.log('server validation failed ', data.validation);
  56. //$yiiform.yiiActiveForm('updateMessages', data.validation, true); // renders validation messages at appropriate places
  57. } else {
  58. // incorrect server response
  59. }
  60. })
  61. .fail(function () {
  62. //alert('Fail message here');
  63. });
  64. function goNext(){
  65. $(".step:eq("+curStep+")").removeClass('active').next().addClass('active');
  66. }
  67. function goNPrev(){
  68. $(".step:eq("+curStep+")").removeClass('active').prev().addClass('active');
  69. }
  70. function validate(){
  71. }
  72. return false; // prevent default form submission
  73. });
  74. //showStep(currentTab);
  75. })(jQuery);
  76. /*
  77. var currentTab = 0; // Current tab is set to be the first tab (0)
  78. showTab(currentTab); // Display the current tab
  79. var orgForm = jQuery('#org-form');
  80. orgForm.on('beforeSubmit', function () {
  81. var $yiiform = $(this);
  82. console.log($(this));
  83. $.ajax({
  84. type: $yiiform.attr('method'),
  85. url: $yiiform.attr('action'),
  86. data: $yiiform.serializeArray(),
  87. }
  88. )
  89. .done(function(data) {
  90. if(data.success) {
  91. // data is saved
  92. $yiiform.find('input[name=id]').val(data.id)
  93. $yiiform.attr('action', '/organizations/update?id='+data.id)
  94. } else if (data.validation) {
  95. // server validation failed
  96. console.log('server validation failed ', data.validation);
  97. //$yiiform.yiiActiveForm('updateMessages', data.validation, true); // renders validation messages at appropriate places
  98. } else {
  99. // incorrect server response
  100. }
  101. })
  102. .fail(function () {
  103. //alert('Fail message here');
  104. })
  105. return false; // prevent default form submission
  106. })
  107. function showTab(n) {
  108. // This function will display the specified tab of the form ...
  109. var x = document.getElementsByClassName("tab");
  110. x[n].style.display = "block";
  111. // ... and fix the Previous/Next buttons:
  112. if (n == 0) {
  113. document.getElementById("prevBtn").style.display = "none";
  114. } else {
  115. document.getElementById("prevBtn").style.display = "inline";
  116. }
  117. if (n == (x.length - 1)) {
  118. document.getElementById("nextBtn").innerHTML = "Submit";
  119. } else {
  120. document.getElementById("nextBtn").innerHTML = "Next";
  121. }
  122. // ... and run a function that displays the correct step indicator:
  123. fixStepIndicator(n);
  124. }
  125. function nextPrev(n) {
  126. // This function will figure out which tab to display
  127. var x = document.getElementsByClassName("tab");
  128. var currentForm = x[currentTab].find('form');
  129. console.log(currentForm);
  130. orgForm.yiiActiveForm('validate', true);
  131. console.log(x[currentTab].getElementsByTagName('form')[0].getAttribute('id'))
  132. // Exit the function if any field in the current tab is invalid:
  133. if (n === 1 && !validateForm()) return false;
  134. //if (!valid) return false;
  135. // Hide the current tab:
  136. x[currentTab].style.display = "none";
  137. // Increase or decrease the current tab by 1:
  138. currentTab = currentTab + n;
  139. // if you have reached the end of the form... :
  140. if (currentTab >= x.length) {
  141. //...the form gets submitted:
  142. //document.getElementById("orgFormContainer").submit();
  143. return false;
  144. }
  145. // Otherwise, display the correct tab:
  146. showTab(currentTab);
  147. }
  148. function validateForm() {
  149. var x, y, i, valid = false;
  150. orgForm.on('afterValidate', function (event, attribute, messages, deferreds) {
  151. if(messages.length>0) valid = false;
  152. });
  153. //valid = document.querySelectorAll('#orgFormContainer input.is-invalid').length <= 0;
  154. console.log(valid);
  155. // This function deals with validation of the form fields
  156. /!*var x, y, i, valid = true;
  157. x = document.getElementsByClassName("tab");
  158. y = x[currentTab].getElementsByTagName("input");
  159. // A loop that checks every input field in the current tab:
  160. for (i = 0; i < y.length; i++) {
  161. // If a field is empty...
  162. if (y[i].value == "") {
  163. // add an "invalid" class to the field:
  164. y[i].className += " invalid";
  165. // and set the current valid status to false:
  166. valid = false;
  167. }
  168. }
  169. // If the valid status is true, mark the step as finished and valid:
  170. if (valid) {
  171. document.getElementsByClassName("step")[currentTab].className += " finish";
  172. }*!/
  173. /!*var valid = false;
  174. valid = document.querySelectorAll('#orgFormContainer input.is-invalid').length > 0 ? false : true;*!/
  175. return valid; // return the valid status
  176. }
  177. function fixStepIndicator(n) {
  178. // This function removes the "active" class of all steps...
  179. var i, x = document.getElementsByClassName("step");
  180. for (i = 0; i < x.length; i++) {
  181. x[i].className = x[i].className.replace(" active", "");
  182. }
  183. //... and adds the "active" class to the current step:
  184. x[n].className += " active";
  185. }*/