test.html 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <title>BootstrapValidator demo</title>
  5. <link rel="stylesheet" href="../vendor/bootstrap/css/bootstrap.css"/>
  6. <link rel="stylesheet" href="http://netdna.bootstrapcdn.com/font-awesome/4.0.3/css/font-awesome.css" />
  7. <link rel="stylesheet" href="../dist/css/bootstrapValidator.css"/>
  8. <script type="text/javascript" src="../vendor/jquery/jquery-1.10.2.min.js"></script>
  9. <script type="text/javascript" src="../vendor/bootstrap/js/bootstrap.min.js"></script>
  10. <script type="text/javascript" src="../dist/js/bootstrapValidator.js"></script>
  11. </head>
  12. <body>
  13. <div class="container">
  14. <div class="row">
  15. <!-- form: -->
  16. <section>
  17. <div class="col-lg-8 col-lg-offset-2">
  18. <div class="page-header">
  19. <h2>Dynamic fields</h2>
  20. </div>
  21. <div class="form-group hide" id="radioTemplate">
  22. <div class="col-md-offset-3 col-md-5">
  23. <label>
  24. <input type="radio" name="elementName" />
  25. Yes
  26. </label>
  27. <label>
  28. <input type="radio" name="elementName" />
  29. No
  30. </label>
  31. </div>
  32. <div class="col-md-4">
  33. <button type="button" class="btn btn-default removeButton">
  34. <i class="fa fa-minus"></i>
  35. </button>
  36. </div>
  37. </div>
  38. <div class="form-group hide" id="textTemplate">
  39. <div class="col-md-offset-3 col-md-5">
  40. <input type="text" name="elementName" class="form-control" />
  41. </div>
  42. <div class="col-md-4">
  43. <button type="button" class="btn btn-default removeButton">
  44. <i class="fa fa-minus"></i>
  45. </button>
  46. </div>
  47. </div>
  48. <div class="container">
  49. <div class="row">
  50. <div class="col-sm-12">
  51. <select id="addType" class="form-control">
  52. <option value="#textTemplate">Add input[type='text']</option>
  53. <option value="#radioTemplate">Add input[type='radio']</option>
  54. </select>
  55. <button type="button" class="btn btn-default addButton" id="addNewElement">
  56. <i class="fa fa-plus"></i>
  57. </button>
  58. <form id="surveyForm" method="post" class="form-horizontal">
  59. <div class="form-group" id="submitFormGroup">
  60. <div class="col-md-5 col-md-offset-3">
  61. <button type="submit" class="btn btn-default">Validate</button>
  62. </div>
  63. </div>
  64. </form>
  65. </div>
  66. </div>
  67. </div>
  68. </div>
  69. </section>
  70. <!-- :form -->
  71. </div>
  72. </div>
  73. <script type="text/javascript">
  74. $(document).ready(function() {
  75. // The maximum number of options
  76. var MAX_OPTIONS = 5;
  77. var ind = 0;
  78. $('#addNewElement').click(function () {
  79. // determine which template to add
  80. var $template = $($('#addType').val()),
  81. $clone = $template
  82. .clone()
  83. .removeClass('hide')
  84. .removeAttr('id')
  85. .attr('data-index', ind),
  86. $option = $clone.find('[name="elementName"]').attr('name', 'field' + ind);
  87. $clone.insertBefore($('#submitFormGroup'));
  88. // Add new field
  89. $('#surveyForm').bootstrapValidator('addField', $option);
  90. ind++;
  91. });
  92. $('#surveyForm')
  93. .bootstrapValidator({
  94. feedbackIcons: {
  95. valid: 'glyphicon glyphicon-ok',
  96. invalid: 'glyphicon glyphicon-remove',
  97. validating: 'glyphicon glyphicon-refresh'
  98. },
  99. fields: {
  100. question: {
  101. validators: {
  102. notEmpty: {
  103. message: 'The question required and cannot be empty'
  104. }
  105. }
  106. },
  107. 'option[]': {
  108. validators: {
  109. notEmpty: {
  110. message: 'The option required and cannot be empty'
  111. },
  112. stringLength: {
  113. max: 100,
  114. message: 'The option must be less than 100 characters long'
  115. }
  116. }
  117. }
  118. }
  119. })
  120. // Remove button click handler
  121. .on('click', '.removeButton', function() {
  122. var $row = $(this).parents('.form-group'),
  123. $ind = $row.attr('data-index'),
  124. $option = $row.find('[name="field' + $ind + '"]');
  125. $('#surveyForm').bootstrapValidator('removeField', $option.eq(0).attr('name'));
  126. // Remove element containing the option
  127. $row.remove();
  128. // Remove field
  129. ind--;
  130. })
  131. // Called after adding new field
  132. .on('added.field.bv', function(e, data) {
  133. // data.field --> The field name
  134. // data.element --> The new field element
  135. // data.options --> The new field options
  136. if (data.field === 'option[]') {
  137. if ($('#surveyForm').find(':visible[name="option[]"]').length >= MAX_OPTIONS) {
  138. $('#surveyForm').find('.addButton').attr('disabled', 'disabled');
  139. }
  140. }
  141. })
  142. // Called after removing the field
  143. .on('removed.field.bv', function(e, data) {
  144. if (data.field === 'option[]') {
  145. if ($('#surveyForm').find(':visible[name="option[]"]').length < MAX_OPTIONS) {
  146. $('#surveyForm').find('.addButton').removeAttr('disabled');
  147. }
  148. }
  149. });
  150. });
  151. </script>
  152. </body>
  153. </html>